Page 2 of 2

Re: Pass values to and get result from JavaScript

Posted: Wed Sep 14, 2016 7:43 am
by QuimV
:D :D
Thank you very much to you both.

Re: Pass values to and get result from JavaScript

Posted: Wed Sep 14, 2016 4:44 pm
by QuimV
:D
Thank you both for your help.

Re: Pass values to and get result from JavaScript

Posted: Mon Mar 23, 2020 1:17 pm
by QuimV
Example of converting an integer representing a date, to a date in normal text format using javascript
(in case it is useful to someone)

Code: Select all


Enumeration Window
  #mainForm
EndEnumeration

Enumeration Gadget
  #HideWeb
EndEnumeration

Global STATIC_HTML.s

OpenWindow(#mainForm, 88, 244, 500, 200, "PB <==> JavasScript", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
WebGadget (#HideWeb, 0, 0, 0, 0, "")

Procedure SetStaticHtml()
  STATIC_HTML = "<html>" + #CRLF$
  STATIC_HTML + #TAB$ + "<meta http-equiv='X-UA-Compatible' content='IE=edge' />" + #CRLF$
  STATIC_HTML + #TAB$ + "<body>" + #CRLF$
  STATIC_HTML + #TAB$ + #TAB$ + "<script>" + #CRLF$
  STATIC_HTML + #TAB$ + #TAB$ + #TAB$ + "var result = new Date(<data>).toLocaleString();" + #CRLF$
  STATIC_HTML + #TAB$ + #TAB$ + #TAB$ + "document.title = result;" + #CRLF$
  STATIC_HTML + #TAB$ + #TAB$ + "</script>" + #CRLF$
  STATIC_HTML + #TAB$ + "</body>" + #CRLF$
  STATIC_HTML + "</html>" + #CRLF$
  Debug STATIC_HTML
EndProcedure

Procedure.s number2date(number)
  Protected html.s, result.s
  
  html = STATIC_HTML
  html = ReplaceString(html,"<data>",Str(number))
  SetGadgetItemText(#HideWeb, #PB_Web_HtmlCode , html)
  delay = ElapsedMilliseconds()
  Repeat
    PageTitle.s = GetGadgetItemText(#HideWeb, #PB_Web_PageTitle)
    If FindString(GetGadgetItemText(#HideWeb, #PB_Web_HtmlCode), HTML) And PageTitle <> "" 
      Break
    EndIf
    WindowEvent()
    Delay(1)
  ForEver 
  delay = ElapsedMilliseconds() - delay
  Debug "Spent: " + Str(delay) + " Milliseconds"
  
  ProcedureReturn GetGadgetItemText(#HideWeb, #PB_Web_PageTitle)
EndProcedure


SetStaticHtml()
Repeat 
  Debug "Date from JavaScript: " + number2date(1552055511000)
  Delay (3000)
Until WaitWindowEvent() = #PB_Event_CloseWindow



Re: Pass values to and get result from JavaScript

Posted: Mon Mar 23, 2020 6:49 pm
by IdeasVacuum
That's handy QuimV, thanks for sharing. - Hope it didn't take four years to code :mrgreen:

Re: Pass values to and get result from JavaScript

Posted: Mon Mar 23, 2020 8:25 pm
by QuimV
:D