paul.coshott Posted April 21, 2019 Share Posted April 21, 2019 Hi All, I have seen the topics on cookies, but I was hoping uniGUI could access Local Storage. Is this possible, and how do I do it? Thanks for any help, Paul Quote Link to comment Share on other sites More sharing options...
Abaksoft Posted April 22, 2019 Share Posted April 22, 2019 Hello Paul, • A good documentation with javascript examples to use localStorage : https://diveintohtml5.info/storage.html • But if you are trying to get or set files from client side HD, there is no conventional way to do it (web security). Quote Link to comment Share on other sites More sharing options...
paul.coshott Posted April 22, 2019 Author Share Posted April 22, 2019 Thanks heaps for the help. Appreciate it. Cheers, Paul Quote Link to comment Share on other sites More sharing options...
paul.coshott Posted April 22, 2019 Author Share Posted April 22, 2019 Quick question - where should i put the java script code to save to local storage and where should the code go to read? Thanks, Paul Quote Link to comment Share on other sites More sharing options...
Abaksoft Posted April 22, 2019 Share Posted April 22, 2019 Hayri ASLAN is specialized in JavaScript and can help you on LocalStorage.. Regards. Quote Link to comment Share on other sites More sharing options...
Hayri ASLAN Posted April 23, 2019 Share Posted April 23, 2019 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"]); 1 Quote Link to comment Share on other sites More sharing options...
Abaksoft Posted April 23, 2019 Share Posted April 23, 2019 Thank you Hayri ASLAN, Interesting ! But where in Delphi, do we put this ? A simple Example please : Unibutton1 --> Click --> Set UniButton2 --> Click --> Get UniButton3 --> Click --> Remove Best regards Quote Link to comment Share on other sites More sharing options...
Abaksoft Posted April 24, 2019 Share Posted April 24, 2019 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 Quote Link to comment Share on other sites More sharing options...
Sherzod Posted April 24, 2019 Share Posted April 24, 2019 8 minutes ago, Abaksoft said: Any idea Sherzod ? Hi, OK, I will check too Quote Link to comment Share on other sites More sharing options...
Sherzod Posted April 24, 2019 Share Posted April 24, 2019 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; 1 Quote Link to comment Share on other sites More sharing options...
Sherzod Posted April 24, 2019 Share Posted April 24, 2019 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; Quote Link to comment Share on other sites More sharing options...
Abaksoft Posted April 24, 2019 Share Posted April 24, 2019 Thank you Sherzod for the fast reply, But to display the value on UniEdit1.Text Nothing !? Quote Link to comment Share on other sites More sharing options...
Sherzod Posted April 24, 2019 Share Posted April 24, 2019 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. Quote Link to comment Share on other sites More sharing options...
Abaksoft Posted April 24, 2019 Share Posted April 24, 2019 OK thx Quote Link to comment Share on other sites More sharing options...
Abaksoft Posted April 25, 2019 Share Posted April 25, 2019 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 4 1 Quote Link to comment Share on other sites More sharing options...
Sherzod Posted April 25, 2019 Share Posted April 25, 2019 Thanks for sharing Quote Link to comment Share on other sites More sharing options...
paul.coshott Posted April 25, 2019 Author Share Posted April 25, 2019 Thanks so much to Abaksoft and Hayri for your help with learning how to use local storage. Great stuff. Cheers, Paul Quote Link to comment Share on other sites More sharing options...
eduardosuruagy Posted April 25, 2019 Share Posted April 25, 2019 What is the difference between LocalStorage and Cookies? Quote Link to comment Share on other sites More sharing options...
Hayri ASLAN Posted April 25, 2019 Share Posted April 25, 2019 Difference Between Local Storage, Session Storage And Cookies 1 Quote Link to comment Share on other sites More sharing options...
eduardosuruagy Posted April 25, 2019 Share Posted April 25, 2019 Thank you Quote Link to comment Share on other sites More sharing options...
paul.coshott Posted April 27, 2019 Author Share Posted April 27, 2019 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 Quote Link to comment Share on other sites More sharing options...
Hayri ASLAN Posted April 27, 2019 Share Posted April 27, 2019 Because you will set it in client side, you must use onAfterShow. After onAfterShow, we are sure elements are rendered. Quote Link to comment Share on other sites More sharing options...
paul.coshott Posted April 29, 2019 Author Share Posted April 29, 2019 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")')]); Quote Link to comment Share on other sites More sharing options...
Sherzod Posted April 29, 2019 Share Posted April 29, 2019 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"))')]); Quote Link to comment Share on other sites More sharing options...
paul.coshott Posted April 30, 2019 Author Share Posted April 30, 2019 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.