BBjHtmlView::injectUrl

Description

In BBj 17.0 and higher, this method injects the specified JavaScript URL into the HTML document of this BBjHtmlView control, making it available for subsequent use by client-side JavaScript, BBjHtmlView::executeScript, or BBjHtmlView::executeAsyncScript.

Syntax

Return Value

Method

void

injectUrl(string url)

void

injectUrl(string url, boolean top)

Parameters

Return Value

Method

url

URL containing JavaScript  to be injected into the HTML document of  this BBjHtmlView.

top

Boolean value specifying whether this URL is to be injected into the top level window of the page.

Return Value

None.

Remarks

In BBj 19.20 and higher, the ON_SCRIPT_LOADED and ON_SCRIPT_FAILED events report the success or failure of loading the requested JavaScript URL into the current page.

Example 1 (BBj 17 and higher)

rem ' BBjHtmlView::injectURL
bui = info(3,6)="5"
client = 0
if info(3,6)<>"5" 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()
window! = sysgui!.addWindow(50,50,300,250,"injectUrl",$00090003$)
window!.setCallback(window!.ON_CLOSE,"eoj")
js$ = "https://www.poweredbybbj.com/files/scripts/hello.js"
html$ = "<html>"
html$ = html$+"<body>"
html$ = html$+"<input type='button' onclick='hello()' value='Click Me!' style='margin:15px;'>"
rem html$ = html$+"<script src="+js$+"></script>"
html$ = html$+"</body></html>"
htmlview! = window!.addHtmlView(101,25,25,250,150,html$)
htmlview!.setCallback(htmlview!.ON_PAGE_LOADED,"inject")
button! = window!.addButton(1,25,200,250,25,"Click Me!",$$)
button!.setCallback(button!.ON_BUTTON_PUSH,"click")
process_events
eoj:
release
inject:
htmlview!.clearCallback(htmlview!.ON_PAGE_LOADED)
htmlview!.injectUrl(js$,1)
wait 0
gosub click; rem ' this doesn't work because the script isn't yet available
return
click:
wnd$ = iff(bui,"$wnd.","window.")
htmlview!.executeScript(wnd$+"hello()")
return

Example 2 (BBj 19.20 and higher)

rem ' BBjHtmlView::injectURL
bui = info(3,6)="5"
client = 0
if info(3,6)<>"5" 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
js$ = "https://www.poweredbybbj.com/files/scripts/hello.js"
bad$ = "https://www.poweredbybbj.com/files/scripts/oops.js"
injectUrl = msgbox("injectUrl Good or Bad JavaScript?",7+32+512,"injectUrl","Good","Bad")
sysgui = unt
open (sysgui)"X0"
sysgui! = bbjapi().getSysGui()
window! = sysgui!.addWindow(50,50,300,250,"injectUrl",$00090003$)
window!.setCallback(window!.ON_CLOSE,"eoj")
html$ = "<html>"
html$ = html$+"<body>"
html$ = html$+"<input type='button' onclick='hello()' value='Click Me!' style='margin:15px;'>"
rem html$ = html$+"<script src="+js$+"></script>"
html$ = html$+"</body></html>"
htmlview! = window!.addHtmlView(101,25,25,250,150,html$)
print date(0:"%Hz:%mz:%sz.%tz %p ") + htmlview!.getClientType()," ",htmlview!.getClientVersion()
htmlview!.setCallback(htmlview!.ON_PAGE_LOADED,"page")
htmlview!.setCallback(htmlview!.ON_SCRIPT_LOADED,"loaded")
htmlview!.setCallback(htmlview!.ON_SCRIPT_FAILED,"failed")
htmlview!.setCallback(htmlview!.ON_EXECUTE_SCRIPT,"script")
button! = window!.addButton(1,25,200,250,25,"Click Me!",$$)
button!.setCallback(button!.ON_BUTTON_PUSH,"click")
process_events
eoj:
release
page:
event! = sysgui!.getLastEvent()
print date(0:"%Hz:%mz:%sz.%tz %p "),event!.getEventName()," ",event!.getUrl()
htmlview!.clearCallback(htmlview!.ON_PAGE_LOADED)
if injectUrl = 1 then
   htmlview!.injectUrl(js$,1)
else
   htmlview!.injectUrl(bad$,1)
endif
return
loaded:
event! = sysgui!.getLastEvent()
print date(0:"%Hz:%mz:%sz.%tz %p "),event!.getEventName()," ",event!.getUrl()
gosub click; rem ' this works because we waited until the script was loaded
return
failed:
event! = sysgui!.getLastEvent()
print date(0:"%Hz:%mz:%sz.%tz %p "),event!.getEventName()," ",event!.getUrl()
return
click:
wnd$ = iff(bui,"$wnd.","window.")
htmlview!.executeScript(wnd$+"hello()")
return
script:
event! = sysgui!.getLastEvent()
print date(0:"%Hz:%mz:%sz.%tz %p "),event!.getEventName()," ",
print event!.getScript(),": ",event!.getResult()
return

Contents of https://www.poweredbybbj.com/files/scripts/hello.js

function hello() {
alert("Hello from an external JavaScript file")
}

See Also

BBjAPI

BBjSysGui

BBjControl

BBjWindow

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



______________________________________________________________________________________

Copyright BASIS International Ltd. BBj®, Visual PRO/5®, PRO/5®, and BBx® are registered trademarks.