Page 1 of 1
Javascript and webgadget
Posted: Mon Aug 06, 2012 11:11 am
by captain_skank
What's the best way to run javascript within the webgadget.
I'd like to show/hide a <DIV> within the webgadget ??
Anybody got a working javascript/webgadget example
cheers
Re: Javascript and webgadget
Posted: Mon Aug 06, 2012 11:21 am
by luis
Try to see if this code can help you:
http://www.purebasic.fr/english/viewtop ... 13&t=45907
You'll see some javascript code stuffed inside a PB string and then executed.
Re: Javascript and webgadget
Posted: Mon Aug 06, 2012 9:16 pm
by Nubcake
I used to use this code too but wouldn't it be easier to use SetGadgetText(WebGadget,"javascript: (javascript goes here)") ?
Re: Javascript and webgadget
Posted: Mon Aug 06, 2012 10:43 pm
by luis
Sure it's easier, but I believe it's not the same thing. It depends.
I'm not so sure it's possible to execute the script and get the control back immediately like using the IHTMLWindow2 object.
Or execute different scripts sequentially (without a refresh... not sure).
But I haven't tried.
Anyway it's a good suggestion if it works ok for the captain, and you were right to point that out, thanks.

Re: Javascript and webgadget
Posted: Tue Aug 07, 2012 12:18 am
by srod
Using COMate_PLUS :
Code: Select all
IncludePath "..\"
XIncludeFile "COMatePLUS.pbi"
Enumeration
#Win1
#Web1
#B1
EndEnumeration
#HTML = "<html><head></head><body><h1>HELLO!</h1><div id=" + Chr(34) + "test" + Chr(34) + "><h2>Click button to toggle visibility!</h2></div></body></html>"
Procedure.i ExecuteJavaScript(Gadget, command$)
Protected browser.COMateObject, documentDispatch.COMateObject, script.COMateObject
Protected result
browser = COMate_WrapCOMObject(GetWindowLongPtr_(GadgetID(gadget), #GWL_USERDATA))
If browser
documentDispatch = browser\GetObjectProperty("Document")
If documentDispatch
script = documentDispatch\GetObjectProperty("script")
If script
result = script\Invoke("ExecScript('" + command$ + "', 'JavaScript')")
script\release()
EndIf
documentDispatch\Release()
EndIf
browser\Release()
EndIf
ProcedureReturn result
EndProcedure
java$ = "if (document.getElementById('test').style.display != 'none')" + #CRLF$
java$ + "{" + #CRLF$
java$ + "document.getElementById('test').style.display = 'none';" + #CRLF$
java$ + " }else{" + #CRLF$
java$ + "document.getElementById('test').style.display = 'block';" + #CRLF$
java$ + "}"
;COMate_PLUS requires that we use escape characters in place of ' characters.
java$ = ReplaceString(java$, "'", "$0027")
If OpenWindow(0, #PB_Ignore, #PB_Ignore, 440, 280, "win1", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_ScreenCentered)
WebGadget(0, 0, 0, 440, 250, "")
SetGadgetItemText(0, #PB_Web_HtmlCode, #HTML)
While WindowEvent():Wend
ButtonGadget(1, 5, 255, 95, 20, "Click me!")
Repeat
event=WaitWindowEvent()
Select event
Case #PB_Event_Gadget
Select EventGadget()
Case 1
ExecuteJavaScript(0, java$)
EndSelect
EndSelect
Until event=#PB_Event_CloseWindow
EndIf
Click the button to toggle the visibility of the DIV section.
Though to be honest JavaScript could be dispensed with here and we could just use COMatePLUS itself to hide the DIV section.
Re: Javascript and webgadget
Posted: Tue Aug 07, 2012 3:15 am
by RichAlgeni
Beautiful Stephen!
Re: Javascript and webgadget
Posted: Tue Aug 07, 2012 11:39 am
by srod
RichAlgeni wrote:Beautiful Stephen!
Aye, you're not so bad looking yourself...
...wait, that's not what you meant was it?

Re: Javascript and webgadget
Posted: Tue Aug 07, 2012 1:03 pm
by captain_skank
Thanks guys, really helpfull.
Re: Javascript and webgadget
Posted: Tue Aug 07, 2012 7:28 pm
by RichAlgeni
Aye, you're not so bad looking yourself...
...wait, that's not what you meant was it?

Um, ya, that too!

Re: Javascript and webgadget
Posted: Wed Aug 08, 2012 3:20 pm
by captain_skank
Nubcake wrote:luis wrote:
I used to use this code too but wouldn't it be easier to use SetGadgetText(WebGadget,"javascript: (javascript goes here)") ?
How would this work, i've tried the following ( hacked from srod's code ) but it keeps throwing an error ?
Code: Select all
java.s = ""
java = "if (document.getElementById('test').style.display != 'none')" + #CRLF$
java + "{" + #CRLF$
java + "var divid = document.getElementById('test');" + #CRLF$
java + "divid.scrollIntoView(true)" + #CRLF$
java + "}"
java = ReplaceString(java, "'", "$0027")
If OpenWindow(0, #PB_Ignore, #PB_Ignore, 440, 120, "win1", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_ScreenCentered)
WebGadget(0, 0, 0, 440, 90, "")
SetGadgetItemText(0, #PB_Web_HtmlCode, "<html><head></head><body><h1>HELLO!</h1><div id='test'><h2>Click button To toggle visibility!</h2></div><h1><a name='top'></a>HELLO</h1></body></html>"); #HTML)
While WindowEvent():Wend
ButtonGadget(1, 5, 100, 95, 20, "Click me!")
Repeat
Event=WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case 1
SetGadgetText(0, "javascript: (" + java$ + ")")
;ExecuteJavaScript(0, java$)
EndSelect
EndSelect
Until Event=#PB_Event_CloseWindow
EndIf
Re: Javascript and webgadget
Posted: Wed Aug 08, 2012 3:34 pm
by luis
Code: Select all
java.s = ""
java = "if (document.getElementById('test').style.display != 'none')" + #CRLF$
java + "{" + #CRLF$
java + "var divid = document.getElementById('test');" + #CRLF$
java + "divid.scrollIntoView(true);" + #CRLF$
java + "}"
; java = ReplaceString(java, "'", "$0027") ; not needed
Debug java
If OpenWindow(0, #PB_Ignore, #PB_Ignore, 440, 120, "win1", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_ScreenCentered)
WebGadget(0, 0, 0, 440, 90, "")
SetGadgetItemText(0, #PB_Web_HtmlCode, "<html><head></head><body><h1>HELLO!</h1><div id='test'><h2>Click button To toggle visibility!</h2></div><h1><a name='top'></a>HELLO</h1></body></html>"); #HTML)
While WindowEvent():Wend
ButtonGadget(1, 5, 100, 95, 20, "Click me!")
Repeat
Event=WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case 1
SetGadgetText(0, "javascript: " + java) ; use EnableExplicit java$ is not java.s, and you don't need parenthesis
;ExecuteJavaScript(0, java$)
EndSelect
EndSelect
Until Event=#PB_Event_CloseWindow
EndIf
Why so many people ignore EnableExplicit and then are bitten for not using it ?
Mistery!
Re: Javascript and webgadget
Posted: Wed Aug 08, 2012 4:01 pm
by captain_skank
DOH !
Cheers