Page 1 of 1

Sending javascript objects to Harbour hashes

PostPosted: Sat May 02, 2020 9:20 am
by Antonio Linares
live demo:
https://www.modharbour.org/modharbour_samples/js_to_prg.prg

source code:
https://github.com/FiveTechSoft/mod_harbour/blob/master/samples/js_to_prg.prg

We also provide the demo to convert Harbour hashes into javascript objects:

live demo:
https://www.modharbour.org/modharbour_samples/prg_to_js.prg

source code:
https://github.com/FiveTechSoft/mod_harbour/blob/master/samples/prg_to_js.prg

This way you clear understand how to send values from Harbour to javascript and from javascript to Harbour

Harbour for the web power! :-)

Re: Sending javascript objects to Harbour hashes

PostPosted: Sat May 02, 2020 11:15 am
by Taavi
Hello Antonio,
what is the way to access localStorage.getItem() /and other javascript objects/ directly from mod_harbour code?

Taavi.

Re: Sending javascript objects to Harbour hashes

PostPosted: Sat May 02, 2020 1:26 pm
by Antonio Linares
Taavi,

Code: Select all  Expand view
function Main()

   LocalStorageSetItem( "Name", "Taavi" )

   ShowLocalStorageItem( "Name" )

return nil  

function LocalStorageSetItem( cKey, cValue )

return AP_RPuts( "<script>localStorage.setItem( '" + cKey + "','" + cValue + "')</script>" )

function ShowLocalStorageItem( cKey )

return AP_RPuts( "<script>alert( localStorage.getItem( '" + cKey + "') )</script>" )

Re: Sending javascript objects to Harbour hashes

PostPosted: Sun May 03, 2020 2:36 pm
by Taavi
Is it possible to get value instead of showing it with js alert?

Something like this?

function Main()
local cname

LocalStorageSetItem( "Name", "Taavi" )

cname:=LocalStorageGetItem( "Name" )

return nil

Re: Sending javascript objects to Harbour hashes

PostPosted: Sun May 03, 2020 3:45 pm
by Antonio Linares
Dear Taavi,

The PRG runs on the server, where there is no "localStorage".

The "locaStorage" lives in the client side, where there are no PRGs, and we only have HTML and javascript.

When you use LocalStorageGetItem( "Name" ), we are creating a javascript script that will run on the client side.

So there is no way to assign its result to a PRG variable as by that time mod_harbour has ended its work and the action is at the client side.

What you can do is to send the value in "localStorage" back to the server, exactly using the technique that I have posted in this thread.

Re: Sending javascript objects to Harbour hashes

PostPosted: Sun May 03, 2020 6:16 pm
by Taavi
Thanks. Antonio.

...just wanted to be sure there is no other way...

Seems we've got our first simple mod_harbour project up and running :-). Keep learning here...

Taavi.