Javascript and webgadget

Just starting out? Need help? Post your questions and find answers here.
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 639
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Javascript and webgadget

Post 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
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Javascript and webgadget

Post 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.
"Have you tried turning it off and on again ?"
A little PureBasic review
Nubcake
Enthusiast
Enthusiast
Posts: 195
Joined: Thu Feb 03, 2011 7:44 pm

Re: Javascript and webgadget

Post by Nubcake »

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)") ?
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Javascript and webgadget

Post 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. :wink:
"Have you tried turning it off and on again ?"
A little PureBasic review
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Javascript and webgadget

Post 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.
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.
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: Javascript and webgadget

Post by RichAlgeni »

Beautiful Stephen!
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Javascript and webgadget

Post by srod »

RichAlgeni wrote:Beautiful Stephen!
Aye, you're not so bad looking yourself...

...wait, that's not what you meant was it?

:D
I may look like a mule, but I'm not a complete ass.
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 639
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: Javascript and webgadget

Post by captain_skank »

Thanks guys, really helpfull.
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: Javascript and webgadget

Post by RichAlgeni »

Aye, you're not so bad looking yourself...

...wait, that's not what you meant was it?

:D
Um, ya, that too!

:shock:
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 639
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: Javascript and webgadget

Post 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
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Javascript and webgadget

Post 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 ? :wink:

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

Re: Javascript and webgadget

Post by captain_skank »

DOH ! :oops:

Cheers
Post Reply