### Use WebEngine executeScript

To execute JavaScript functions, use the WebEngine executeScript method.
More information can be found here:
http://docs.oracle.com/javafx/2/api/javafx/scene/web/WebEngine.html#executeScript(java.lang.String)
There are a variety of JavaScript Categories withe related methods, like history, location etc.
WebEngine JavaScript History Methods
• history.back() = Back in History
• history.forward() = Forward in History
• history.go() = Go to entry in History
• history.length = Get number of pages visited
This is a generic Sub to execute JavaScript History methods
' Execute JavaScript History command: back(), forward(), go(+/-n), length
' Returns an Object which can be used further if required
`Sub runJSHistory(wv As WebView, cmd As String) As Object
If cmd.Length = 0 Then Return Null
cmd = "history." & cmd
Try
Dim joWV As JavaObject = wv
Return joWV.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array As String(cmd))
Catch
lblInformation.Text = LastException.Message
Return Null
End Try
End Sub`
Examples:
• Pages visited: runJSHistory(wvwMain, "length")
• Go back in History: runJSHistory(wvwMain, "back()")
• Go Forward in History: runJSHistory(wvwMain, "forward()")
Alternative Subs
If instead of the generic Sub a dedicated Sub is requested, this is an example:
' Go back in URL history.
' Makes use of the WebEngine executeScript method to execute JavaScript
`Sub goBackHistory(wv As WebView)
Try
Dim joWV As JavaObject = wv
joWV.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array As String("history.back()"))
Catch
lblInformation.Text = LastException.Message
End Try
End Sub`
' Go number of URLs forward (+) or backwards (-) in URL history.
' Makes use of the WebEngine executeScript method to execute JavaScript
`Sub goWebPageGo(wv As WebView, target As Int)
Try
Dim joWV As JavaObject = wv
Dim s As String = "history.go(" & target & ")"
joWV.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array As String(s))
Catch
lblInformation.Text = LastException.Message
End Try
End Sub`
' Get the current web page location loaded in a webview
' Parameter: WebView
' Returns WebPage Location
`Sub getWebPageLocation(wv As WebView) As String
Dim result As String = ""
Try
Dim joWV As JavaObject = wv
Return joWV.RunMethodJO("getEngine", Null).RunMethod("getLocation", Null)
Catch
result = LastException.Message
End Try
Return result
End Sub`
`Sub setJSEnabled(wv As WebView) As String
Dim result As String = ""
Try
Dim joWV As JavaObject = wv
Return joWV.RunMethodJO("getEngine", Null).RunMethod("setJavaScriptEnabled", Array As Object(True))
Catch
result = LastException.Message
End Try
Return result
End Sub`

Changed: , Category: WebView, File: T6C284M2140.htm, ID: 2140