Jump to content

How can I access Local Storage


paul.coshott

Recommended Posts

To set item in local storage 

JSCallGlobal('localStorage.setItem', ["lastname", "Smith"]);

Then you can use it in client side 

var lastname = localStorage.getItem("lastname");

to remove item

JSCallGlobal('localStorage.removeItem', ["lastname"]);

 

  • Upvote 1
Link to comment
Share on other sites

Apologize for my JavaScript ignorance  :(

 

1.  I define On Main  :   Script

function setItem(key, value) {
  localStorage.setItem(key, value);
};
    

function getItem(key) {
  return localStorage.getItem(key);
};

 

 

2. Then  to Set  / Get  :     but  Not work !?


// To Set the Key "lastname"
procedure TMainForm.UniButton1Click(Sender: TObject);
begin
  UniButton1.JSInterface.JSCallGlobal('setItem', ['"lastname"', '"Smith"'] );
end;



// To retreive Value of "lastname" on UniEdit1
procedure TMainForm.UniButton2Click(Sender: TObject);
begin
  UniEdit1.JSInterface.JSCallGlobal('getItem', ['"lastname"']);
end;


// To remove Key
procedure TMainForm.UniButton3Click(Sender: TObject);
begin
  UniButton3.JSInterface.JSCallGlobal('localStorage.removeItem', ['"lastname"']);
end;

 

Any idea Sherzod ?

Thx

Link to comment
Share on other sites

10 minutes ago, Abaksoft said:

// To Set the Key "lastname" procedure TMainForm.UniButton1Click(Sender: TObject); begin UniButton1.JSInterface.JSCallGlobal('setItem', ['"lastname"', '"Smith"'] ); end;

This is correct, without the double quotes :

// To Set the Key "lastname"
procedure TMainForm.UniButton1Click(Sender: TObject);
begin
  UniButton1.JSInterface.JSCallGlobal('setItem', ['lastname', 'Smith'] );
end;

 

  • Upvote 1
Link to comment
Share on other sites

13 minutes ago, Abaksoft said:

// To remove Key procedure TMainForm.UniButton3Click(Sender: TObject); begin UniButton3.JSInterface.JSCallGlobal('localStorage.removeItem', ['"lastname"']); end;

Also without the double quotes :

// To remove Key
procedure TMainForm.UniButton3Click(Sender: TObject);
begin
  UniButton3.JSInterface.JSCallGlobal('localStorage.removeItem', ['lastname']);
end;

 

Link to comment
Share on other sites

45 minutes ago, Abaksoft said:

// To retreive Value of "lastname" on UniEdit1 procedure TMainForm.UniButton2Click(Sender: TObject); begin UniEdit1.JSInterface.JSCallGlobal('getItem', ['"lastname"']); end;

To do this, I think you need to have one more property on the server side too for synchronization.

Link to comment
Share on other sites

Hello Friends,

Finally, a big thx to Hayri ASLAN,  who corrected my code.

Thank you to everyone who participated in this topic : Paul, Sherzod and Hayri

 

For those who are interested

//**********************************************************************
// METHODE  without define (JavaScript Code)  on Main >  Script
// Big Thx to Hayri ASLAN  [Unigui Forum  24/04/2019]


//  Benefict to Use LocalStorage instead of Cookies :
//  10 Mo  Persistent data on Client Side  (even with Refresh browser)
//  Only strings are store

//**********************************************************************



//1. To Set the Key 'K1'
procedure TMainForm.UniButton1Click(Sender: TObject);
var st:string;
begin
  st:=UniEdit1.Text;
  UniButton1.JSInterface.JSCallGlobal('localStorage.setItem', ['K1', st] );   // Key , Value  (always string)
end;



//2. To retreive Value of 'K1' on UniEdit2
procedure TMainForm.UniButton2Click(Sender: TObject);
begin

  with UniEdit2.JSInterface do
   JSCall('setValue', [jsstatement('localStorage.getItem("K1")')]);      // important : Key must be Quoted " "

end;




//3. To remove Key 'K1'
procedure TMainForm.UniButton3Click(Sender: TObject);
begin
  UniButton3.JSInterface.JSCallGlobal('localStorage.removeItem', ['K1']);
end;

 

All the Best :)

LocalStorage2.zip

  • Like 4
  • Upvote 1
Link to comment
Share on other sites

Hi Guys,

I got the example code above from Abaksoft working, but I'm not sure about the best way to use the code in my situation.

I'm simply trying to save the position of a splitter when the user closes the form, and then retrieve that position when the user opens the form next.

Say I've got :

panLeft - TUniPanel : alignment set to alLeft

splitter1 - TUniSplitter : alignment set to alLeft

panClient - TUniPanel : alignment set to alClient

I'm doing the setItem in the OnClose event of the form. Is this the correct place, and what code should I use?

And then in the OnBeforeShow event, I'm trying to do the getItem. Should this be in the OnBeforeShow or OnAfterShow? And how do I code to reset the splitter position?

Thanks for any help,
Paul

Link to comment
Share on other sites

Hi Hayri,

Thanks for the answer.

Could you also tell me what control I should use to return an integer (saved as a string) that will be used to set the width of a panel. I can't do the following, but this is what I need. How do I do something like this?

Thanks,
Paul

  panelLeft.Width := ????.JSInterface.JSCall('setValue', [????.JSInterface.JSStatement('localStorage.getItem("K1")')]);
Link to comment
Share on other sites

13 minutes ago, paul.coshott said:

  panelLeft.Width := ????.JSInterface.JSCall('setValue', [????.JSInterface.JSStatement('localStorage.getItem("K1")')]);

Hello,

Well, you can try something like this:

panelLeft.JSInterface.JSCall('setWidth', [panelLeft.JSInterface.JSStatement('parseInt(localStorage.getItem("K1"))')]);

 

Link to comment
Share on other sites

On 4/29/2019 at 6:17 PM, Sherzod said:

panelLeft.JSInterface.JSCall('setWidth', [panelLeft.JSInterface.JSStatement('parseInt(localStorage.getItem("K1"))')]);

Hi Sherzod, this isn't working for me. Could you please check the syntax to see if you can spot any issue. If not, I'll create a small example app that try's to use the code.

Thanks,
Paul

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...