Javascript and webgadget
- captain_skank
- Enthusiast
- Posts: 639
- Joined: Fri Oct 06, 2006 3:57 pm
- Location: England
Javascript and webgadget
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
I'd like to show/hide a <DIV> within the webgadget ??
Anybody got a working javascript/webgadget example
cheers
Re: Javascript and webgadget
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.
You'll see some javascript code stuffed inside a PB string and then executed.
"Have you tried turning it off and on again ?"
A little PureBasic review
A little PureBasic review
Re: Javascript and webgadget
luis wrote: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.
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
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.
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.

"Have you tried turning it off and on again ?"
A little PureBasic review
A little PureBasic review
Re: Javascript and webgadget
Using COMate_PLUS :
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.
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
Though to be honest JavaScript could be dispensed with here and we could just use COMatePLUS itself to hide the DIV section.
Last edited by srod on Tue Aug 07, 2012 11:41 am, edited 2 times in total.
I may look like a mule, but I'm not a complete ass.
- RichAlgeni
- Addict
- Posts: 935
- Joined: Wed Sep 22, 2010 1:50 am
- Location: Bradenton, FL
Re: Javascript and webgadget
Beautiful Stephen!
Re: Javascript and webgadget
Aye, you're not so bad looking yourself...RichAlgeni wrote:Beautiful Stephen!
...wait, that's not what you meant was it?

I may look like a mule, but I'm not a complete ass.
- captain_skank
- Enthusiast
- Posts: 639
- Joined: Fri Oct 06, 2006 3:57 pm
- Location: England
Re: Javascript and webgadget
Thanks guys, really helpfull.
- RichAlgeni
- Addict
- Posts: 935
- Joined: Wed Sep 22, 2010 1:50 am
- Location: Bradenton, FL
Re: Javascript and webgadget
Um, ya, that too!Aye, you're not so bad looking yourself...
...wait, that's not what you meant was it?

- captain_skank
- Enthusiast
- Posts: 639
- Joined: Fri Oct 06, 2006 3:57 pm
- Location: England
Re: Javascript and webgadget
How would this work, i've tried the following ( hacked from srod's code ) but it keeps throwing an error ?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)") ?
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
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

Mistery!
"Have you tried turning it off and on again ?"
A little PureBasic review
A little PureBasic review
- captain_skank
- Enthusiast
- Posts: 639
- Joined: Fri Oct 06, 2006 3:57 pm
- Location: England
Re: Javascript and webgadget
DOH !
Cheers

Cheers