BBjSysGui::injectScript

Description

In BBj 21.10 and higher, this method injects the specified JavaScript into the web page (see Remarks), making it available for subsequent use by BBjSysGui::executeScript.

Syntax

Return Value Method
void injectScript(string script)
void injectScript(string script, boolean top)

Parameters

Variable Description
script JavaScript to be injected into this web page.
top

A boolean value specifying whether this script is to be injected into the top level window of the page.

Return Value

None.

Remarks

In BBj 22.02 and higher, a subset of this functionality is available in the GUI client. Because the client isn't a web page, user-facing components, including Window, Document, and functions like alert, aren't available.

Example 1 (BUI and DWC)

rem ' BBjSysGui::injectScript Example: BUI and DWC

rem ' BBjSysGui::injectScript
bui = info(3,6)="5"
dwc = info(3,6)="6"
sysgui = unt
open (sysgui)"X0"
sysgui! = bbjapi().getSysGui()
window! = sysgui!.addWindow(50,50,300,200,"injectScript",$00090003$)
window!.setCallback(window!.ON_CLOSE,"eoj")
executeScript! = window!.addButton(1,25,25,250,25,"executeScript")
executeScript!.setCallback(executeScript!.ON_BUTTON_PUSH,"executeScript")
js! = window!.addStaticText(101,25,75,250,100,"",$$)
script$ = "function testjs(parameter1){alert('testjs: '+parameter1);}"
sysgui!.injectScript(script$,1)
doc$ = iff(bui,"$doc","document")
wnd$ = iff(bui,"$wnd","window")
process_events

eoj:
release
return

executeScript:
    r = rnd(255)
    g = rnd(255)
    b = rnd(255)
    rgb$ = "#"+hta(chr(r))+hta(chr(g))+hta(chr(b))
    js$ = doc$+".body.style.backgroundColor = """+rgb$+""""
    print js$
    js!.setText(js$)
    print sysgui!.executeScript(js$)
    print sysgui!.executeScript(wnd$+".testjs('"+js$+"')")
return

Example 2 (All Clients)

rem ' BBjSysGui::injectScript

sysgui = unt
open (sysgui)"X0"
sysgui! = bbjapi().getSysGui()
window! = sysgui!.addWindow(25,25,500,175,"toISOString",$00090083$)
window!.setCallback(window!.ON_CLOSE,"eoj")
window!.addStaticText(101,25,28,120,25,"Date String:",$8000$)
text$ = "December 25, " + date(0:"%Yl")
text! = window!.addEditBox(102,150,25,325,25,text$,$$)
window!.addStaticText(103,25,78,120,25,"ISO String:",$8000$)
result! = window!.addEditBox(104,150,75,325,25,"",$0200$)
result!.setBackColor(BBjColor.WHITE)

rem ' https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date
js$ = "function toISOString(date){try{return new Date(date).toISOString()}catch(e){return new String(e)}}"
sysgui!.injectScript(js$)
toISOString! = window!.addButton(1,150,125,150,25,"toISOString",$$)
toISOString!.setCallback(toISOString!.ON_BUTTON_PUSH,"toISOString")
close! = window!.addButton(2,325,125,150,25,"Close",$$)
close!.setCallback(close!.ON_BUTTON_PUSH,"eoj")
process_events

eoj:
release

toISOString:
    text$ = text!.getText()
    script$ = "toISOString('"+text$+"')"
    ISOString! = sysgui!.executeScript(script$)
    result!.setText(str(ISOString!))
return

ClosedVersion History

  • BBj 21.10: BBjSysGui::injectScript introduced.
  • BBj 22.02: Added a subset of this functionality in the GUI client.

See Also

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

BBjAPI

BBjSysGui

BBjSysGui::executeScript