BrowserWindow and DomElement.
Evaluates JavaScript code within the context of the top level document. For pages that contain frames, this means that the script will run within the document that contains the frameset definition.
This functionality is supported only if you are using the Open Agent.
object.ExecuteJavaScript(code, elementName)
Variable | Description |
---|---|
code | The JavaScript code to evaluate. STRING. |
elementName |
Optional: A name for the current element that can be used in the script. For example,
alert(currentElement.tagName); opens a message box and displays the current element's tag name.
STRING.
This parameter is only available for the DomElement class. |
BrowserWindow.ExecuteJavaScript() and DomElement.ExecuteJavaScript() basically provide the same functionality for the Open Agent as ExecScript(), ExecMethod(), and ExecLine() do for the Classic Agent.
ExecuteJavaScript forwards directly to the eval() DOM method. Therefore, the string provided to this method can be anything that the JavaScript runtime can evaluate. The method fails if the runtime cannot evaluate the JavaScript code.
This method does not return a value.
In framesets, BrowserWindow.ExecuteJavaScript() always evaluates the code in the top level document. DomElement.ExecuteJavaScript() always evaluates the code in the document that contains the element. Therefore, use DomElement.ExecuteJavaScript() to evaluate code within frames.
The following example calls an existing JavaScript function already contained within the page.
InternetExplorer.Page.ExecuteJavaScript("tfShowMsg(tfForm)") Verify(InternetExplorer.Page.Find(".//input[@name='tfShowChange']").GetText(), "Text changed in left TextField.")
The following example injects new functions into the document and calls them.
STRING strJavaScript = "function InjectedFunction() "{document.getElementsByName('Pets')[0].options[2].selected=true;}" InternetExplorer.Page.ExecuteJavaScript(strJavaScript) InternetExplorer.Page.ExecuteJavaScript("InjectedFunction()") Verify(FindDomElement(".//select[@name='Pets']").GetText(), "Fishes")
The following example triggers a DOM event by checking to see if the default name is currentElement.
FindDomElement(".//a[@innerText='Citrus']").ExecuteJavaScript("currentElement.onmouseover();") Verify(FindDomElement(".//img[@name='imSwitch']").GetProperty("src"), "http://borland.com/HtmlTestPages/images/citrus.jpg")