Pass values to and get result from JavaScript

Just starting out? Need help? Post your questions and find answers here.
QuimV
Enthusiast
Enthusiast
Posts: 337
Joined: Mon May 29, 2006 11:29 am
Location: BARCELONA - SPAIN

Re: Pass values to and get result from JavaScript

Post by QuimV »

:D :D
Thank you very much to you both.
QuimV
QuimV
Enthusiast
Enthusiast
Posts: 337
Joined: Mon May 29, 2006 11:29 am
Location: BARCELONA - SPAIN

Re: Pass values to and get result from JavaScript

Post by QuimV »

:D
Thank you both for your help.
QuimV
QuimV
Enthusiast
Enthusiast
Posts: 337
Joined: Mon May 29, 2006 11:29 am
Location: BARCELONA - SPAIN

Re: Pass values to and get result from JavaScript

Post 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


QuimV
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Pass values to and get result from JavaScript

Post by IdeasVacuum »

That's handy QuimV, thanks for sharing. - Hope it didn't take four years to code :mrgreen:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
QuimV
Enthusiast
Enthusiast
Posts: 337
Joined: Mon May 29, 2006 11:29 am
Location: BARCELONA - SPAIN

Re: Pass values to and get result from JavaScript

Post by QuimV »

:D
QuimV
Post Reply