Page 1 of 1

LocalStorage, SessionStorage

Posted: Sat May 10, 2014 12:15 pm
by eddy
- web storages (Local Storage & Session Storage) have bigger capacity.
- local storage is usefull if you code an "offline" mode
- session storage is like bigger domain-restricted cookie
- Attention, web storage data are not crypted. (e.g. players can read/write their scores stored in web storage.)

Code: Select all

Import ""
  SetLocalStorage(Key.s, Value.f) As "localStorage.setItem"
  GetLocalStorage.f(Key.s) As "localStorage.getItem"
  SetLocalStorageString(Key.s, Value.s) As "localStorage.setItem"
  RemoveLocalStorage(Key.s) As "localStorage.removeItem"
  ClearLocalStorage() As "localStorage.clear"
  
  SetSessionStorage(Key.s, Value.f) As "sessionStorage.setItem"
  GetSessionStorage.f(Key.s) As "sessionStorage.getItem"
  SetSessionStorageString(Key.s, Value.s) As "sessionStorage.setItem"
  RemoveSessionStorage(Key.s) As "sessionStorage.removeItem"
  ClearSessionStorage() As "sessionStorage.clear"
EndImport 

Procedure.s GetLocalStorageString(Key.s) 
  !return localStorage.getItem(v_Key) || '';
EndProcedure
Procedure.s GetSessionStorageString(Key.s) 
  !return sessionStorage.getItem(v_Key) || '';
EndProcedure

Procedure GetLocalStorageKeys(List KeyList.s()) 
  ClearList(KeyList())
  !for(var i=0;i<localStorage.length;i++){
    Protected key.s
    !v_key=localStorage.key(i);
    AddElement(KeyList())
    KeyList()=key
  !} //next 
EndProcedure
Procedure.s GetSessionStorageKeys(List KeyList.s()) 
  ClearList(KeyList())
  !for(var i=0;i<sessionStorage.length;i++){
    Protected key.s
    !v_key=sessionStorage.key(i);
    AddElement(KeyList())
    KeyList()=key
  !} //next 
EndProcedure

Procedure.i FindLocalStorage(Key.s)
  !if (v_Key in localStorage) return 1;
  EndProcedure
  Procedure.i FindSessionStorage(Key.s)
  !if (v_Key in sessionStorage) return 1;
EndProcedure

CompilerIf #PB_Compiler_IsMainFile
  ; *******************************
  ; EXAMPLE 
  ; *******************************
  
  ;local data will persist even after the browser is closed
  If FindLocalStorage("localData")
    Debug "localData exists!"+#LF$+"Get localData = "+GetLocalStorageString("localData")+#LF$
  Else 
    SetLocalStorageString("localData","my string value")
    Debug "Set localData:"+GetLocalStorageString("localData")+#LF$
  EndIf 
  
  ;session data is also persistent data, however the data is tied to a browsing context (i.e. a browser tab Or window)
  If FindSessionStorage("sessionData")
    Debug "sessionData exists!"+#LF$+"Get sessionData = "+GetSessionStorage("sessionData")+#LF$
  Else 
    SetSessionStorage("sessionData",1234)
    Debug "Set sessionData:"+GetSessionStorage("sessionData")+#LF$
  EndIf 
  
  NewList Key$()
  GetLocalStorageKeys(Key$())
  ForEach Key$() : Debug "key:"+Key$() : Next
  GetSessionStorageKeys(Key$())
  ForEach Key$() : Debug "key:"+Key$() : Next
CompilerEndIf 

Re: LocalStorage, SessionStorage

Posted: Mon Oct 17, 2016 9:18 am
by bbanelli
Greetings, I'm getting:

Code: Select all

Uncaught ReferenceError: v_Key is not defined
in this code:

Code: Select all

function f_findlocalstorage(v_key) {
if (v_Key in localStorage) return 1;
return 0;
}
In Chrome 53.0.2785.143 m (64-bit).

Any clues, I'm kinda lost in this JS thing... :)

Re: LocalStorage, SessionStorage

Posted: Mon Oct 17, 2016 9:25 am
by Peter
change all v_* - Variables to lowercase:

Code: Select all

if (v_key in localStorage) return 1;
Greetings ... Peter

Re: LocalStorage, SessionStorage

Posted: Mon Oct 17, 2016 9:45 am
by bbanelli
Hi Peter, thank you very much.

So, "EnableExplicit" has no use in SB for such cases?

Re: LocalStorage, SessionStorage

Posted: Mon Oct 17, 2016 9:58 am
by Peter
bbanelli wrote:So, "EnableExplicit" has no use in SB for such cases?
yes, EnableExplicit is for SpiderBasic-Code only (and not for JavaScript-Code).

Greetings ... Peter

Re: LocalStorage, SessionStorage

Posted: Mon Oct 17, 2016 10:32 am
by bbanelli
Peter wrote:
bbanelli wrote:So, "EnableExplicit" has no use in SB for such cases?
yes, EnableExplicit is for SpiderBasic-Code only (and not for JavaScript-Code).

Code: Select all

Define.s OS, Browser, txt
! v_os = navigator.platform;
! v_browser = navigator.userAgent;
However, this code makes no difference whether SB's variables are case (in)sensitive?

Re: LocalStorage, SessionStorage

Posted: Mon Oct 17, 2016 12:05 pm
by Peter
* SpiderBasic-Variables are always case-insensitive (HelloWorld = helloWORLD)
* JavaScript-Variables are always case-sensitive (HelloWorld <> helloWORLD)

And EnableExplicit considered only SpiderBasic-Variables.

Fred decides to rename all SpiderBasic-Variables to v_[LowerCaseVariableName] when transpiling into JavaScript.

So HelloWorld in SpiderBasic turns to v_helloworld in JavaScript

Greetings ... Peter