BBjHtmlView::executeAsyncScript

Description

In BBj 19.10 and higher, this method executes the specified JavaScript in this BBjHtmlView control and returns immediately. In BBj 22.14 and higher, the return value is an integer index that will be included in a subsequent BBjExecuteScriptEvent.

Syntax

Return Value Method
int executeAsyncScript(String script)
int executeAsyncScript(String script, boolean await)
int executeAsyncScript(String script, boolean await, boolean event)

Parameters

Parameter Description

script

JavaScript to be executed in this BBjHtmlView.

await In BBj 23.04 and higher, this optional boolean value specifies that the DWC client should execute this script using the JavaScript await operator. If not specified, the default is false (0).
event In BBj 24.11 and higher, this optional boolean value specifies that this script's result should be reported in a subsequent BBjExecuteScriptEvent. If not specified, the default is true (1).

Return Value

In BBj 22.14 and higher, the return value is an integer index that will be included in a subsequent BBjExecuteScriptEvent.

Example

rem ' BBjHtmlView::executeAsyncScript
web = info(3,6)="5" or info(3,6)="6"
client = 0
if !web then
   client = msgbox("BBjHtmlView Client",7+32+512,"BBjHtmlView","Swing","JavaFX","Chromium")
   switch client
      case 1; rem ' Swing
         print stbl("!OPTIONS","CHROMIUM_HTMLVIEW=FALSE",ERR=*NEXT)
         print stbl("!OPTIONS","JAVAFX_HTMLVIEW=FALSE",ERR=*NEXT)
         break
      case 2; rem ' JavaFX
         print stbl("!OPTIONS","CHROMIUM_HTMLVIEW=FALSE",ERR=*NEXT)
         print stbl("!OPTIONS","JAVAFX_HTMLVIEW=TRUE",ERR=*NEXT)
         break
      case 3; rem ' Chromium
         print stbl("!OPTIONS","CHROMIUM_HTMLVIEW=TRUE",ERR=*NEXT)
         print stbl("!OPTIONS","JAVAFX_HTMLVIEW=TRUE",ERR=*NEXT)
         break
   swend
endif
sysgui = unt
open (sysgui)"X0"
sysgui! = bbjapi().getSysGui()
bui! = bbjapi().getWebManager()
window! = sysgui!.addWindow(25,25,300,500,"executeAsyncScript",$00090003$)
window!.setCallback(window!.ON_CLOSE,"eoj")
gosub makehtml
htmlview! = window!.addHtmlView(101,25,25,250,150,html$)
htmlview!.setCallback(htmlview!.ON_PAGE_LOADED,"pageloaded")
if web then
   htmlview!.injectScript("function doAlert() { console.log(alert('Alert dialog')); }",1)
   htmlview!.injectScript("function doConfirm() { console.log(confirm('Confirm dialog')); }",1)
   htmlview!.injectScript("function doPrompt() { console.log(prompt('Prompt dialog')); }",1)
endif
print "getClientType: ",htmlview!.getClientType(err=*next)
print "getClientVersion: ",htmlview!.getClientVersion(err=*next)
alert! = window!.addButton(102,25,200,250,25,"alert",$$)
alert!.setCallback(alert!.ON_BUTTON_PUSH,"alert")
confirm! = window!.addButton(103,25,250,250,25,"confirm",$$)
confirm!.setCallback(alert!.ON_BUTTON_PUSH,"confirm")
prompt! = window!.addButton(104,25,300,250,25,"prompt",$$)
prompt!.setCallback(prompt!.ON_BUTTON_PUSH,"prompt")
backgroundColor! = window!.addButton(105,25,350,250,25,"backgroundColor",$$)
backgroundColor!.setCallback(backgroundColor!.ON_BUTTON_PUSH,"backgroundColor")
samples! = window!.addButton(2,25,400,250,25,"Samples",$$)
samples!.setCallback(samples!.ON_BUTTON_PUSH,"samples")
buisamples! = window!.addButton(3,25,450,250,25,"BUI Samples",$$)
buisamples!.setCallback(buisamples!.ON_BUTTON_PUSH,"samples")
buisamples!.setEnabled(web)
process_events
eoj:
release
makehtml:
html$ = ""
html$ = html$ + "<!DOCTYPE html> <html> <body>"
html$ = html$ + "<br><button onclick='doAlert()'>Alert</button>"
html$ = html$ + "<br><button onclick='doConfirm()'>Confirm</button>"
html$ = html$ + "<br><button onclick='doPrompt()'>Prompt</button>"
html$ = html$ + "<script>"
html$ = html$ + "function doAlert() { console.log(alert('Alert dialog')); }"
html$ = html$ + "function doConfirm() { console.log(confirm('Confirm dialog')); }"
html$ = html$ + "function doPrompt() { console.log(prompt('Prompt dialog')); }"
html$ = html$ + "</script>"
html$ = html$ + "</body> </html>"
return
pageloaded:
event! = sysgui!.getLastEvent()
htmlview!.setCallback(htmlview!.ON_EXECUTE_SCRIPT,"executeScript")
bui!.setCallback(bui!.ON_EXECUTE_SCRIPT,"executeScript",err=*next)
i = msgbox(str(event!.getText()),32768,event!.getEventName())
return
alert:
js$ = "alert('alert')"
bui = 0
gosub executeAsyncScript
return
confirm:
js$ = "confirm('confirm')"
bui = 0
gosub executeAsyncScript
return
prompt:
js$ = "prompt('prompt')"
bui = 0
gosub executeAsyncScript
return
executeScript:
  event! = sysgui!.getLastEvent()
  result! = event!.getResult()
  if result! = null() then result$ = "null" else result$ = result!.getClass().getName()+" "+result!.toString()
  index = 0, index = event!.getIndex(err=*next)
  msg$ = event!.getEventName()+" "+str(index)+" "+date(0:"%Hz:%mz:%sz.%tz %p")
  msg$ = msg$ + " result="+result$+" script="+ event!.getScript()+" control="+str(event!.getControl())
  print msg$
  System.err.println(msg$)
  rem i = msgbox(msg$,0,event!.getEventName())
return
backgroundColor:
r = rnd(255)
g = rnd(255)
b = rnd(255)
rgb$ = "#"+hta(chr(r))+hta(chr(g))+hta(chr(b))
js$ = "document.body.style.backgroundColor = """+rgb$+""""
if web then js$ = "style.backgroundColor = """+rgb$+""""
bui = 0
gosub executeAsyncScript
return
executeAsyncScript:
if bui then
   index! = bui!.executeAsyncScript(js$)
else
   index! = htmlview!.executeAsyncScript(js$)
endif
msg$ = "executeAsyncScript "+str(index!)+" "+date(0:"%Hz:%mz:%sz.%tz %p")+": "+js$
print msg$
System.err.println(msg$)
return
samples:
event! = sysgui!.getLastEvent()
button! = event!.getButton()
bui = button!.getID() = 3
if web then body$ = "" else body$ = "document.body."
js$ = "Math.sqrt(100)"; gosub executeAsyncScript; rem ' Double or Integer 10
js$ = "Math.sqrt(-1)"; gosub executeAsyncScript; rem ' Double NaN
js$ = "null"; gosub executeAsyncScript; rem ' null
js$ = "undefined"; gosub executeAsyncScript; rem ' null
js$ = "document.hasFocus()"; gosub executeAsyncScript; rem ' Boolean false
js$ = body$ + "style.backgroundColor"; gosub executeAsyncScript; rem ' String #ffff00
js$ = "Math.PI"; gosub executeAsyncScript; rem ' Double 3.141592653589793
js$ = "1/0"; gosub executeAsyncScript; rem ' Double Infinity
js$ = "-1/0"; gosub executeAsyncScript; rem ' Double -Infinity
js$ = "Number.MIN_SAFE_INTEGER"; gosub executeAsyncScript; rem ' 
js$ = "Number.MAX_SAFE_INTEGER"; gosub executeAsyncScript; rem ' 
js$ = "Number.MAX_VALUE"; gosub executeAsyncScript; rem ' 
js$ = "0"; gosub executeAsyncScript; rem ' Integer
js$ = "42"; gosub executeAsyncScript; rem ' Integer
js$ = "2147483647"; gosub executeAsyncScript; rem ' Integer
js$ = "2147483648"; gosub executeAsyncScript; rem ' Long
js$ = """true"""; gosub executeAsyncScript; rem ' String
js$ = """false"""; gosub executeAsyncScript; rem ' String
js$ = """0"""; gosub executeAsyncScript; rem ' String
js$ = """1"""; gosub executeAsyncScript; rem ' String
js$ = """null"""; gosub executeAsyncScript; rem ' String
js$ = "null"; gosub executeAsyncScript; rem ' null
js$ = "undefined"; gosub executeAsyncScript; rem ' null
return

ClosedVersion History

  • BBj 23.11: Added an optional event argument.
  • BBj 23.04: Added an optional await argument.
  • BBj 22.14: BBjHtmlView::executeAsyncScript now returns an index as an int.
  • BBj 19.10: BBjHtmlView::executeAsyncScript added.

See Also

BBjAPI

BBjSysGui

BBjControl

BBjWindow

See the BBj Object Diagram for an illustration of the relationship between BBj Objects.