How do I Read javascript localStorage inside Construct 2

Get help using Construct 2

Post » Fri Aug 21, 2015 6:04 pm

Hi, All...

I Need a big help regading how to read a variable stored in my .html javascript localStorage.

I have one page that stores some data inside localStorage:
E.g:
<script>
localStorage.setItem("lastname", "Smith");
</script>
I would like to read this variable inside Construct 2.

Is there any tutorial that could lead me on how to do this?

Thanks in advance!!
B
3
Posts: 1
Reputation: 177

Post » Tue Mar 01, 2016 2:54 pm

Hey !

Same problem here. I've tried to get data from local storage into another page after setting it into Construct2 and vice versa.

It seems Construct2 developpers use a standard javascript library called "Local Forage" (and not Local Storage). This library is similar to local storage but use asynchronous mechanisms and other storage methods like WebSQL or IndexedDB. That's probably why we need to wait specific events to retrieve our data and that's why we cannot get/set the information using local storage only.

So if you need to set data into your HTML Page and use it into Construct2, you can check out the local forage library (available here : https://github.com/mozilla/localForage). Follow the examples available in the github page. For me it works like a charm. :)

It could be useful if a Scirra developper may confirm you can use local forage library safely in external web pages to get Construct2 information. It could be dangerous if they decide to make a bridge with specific keys or if the storage priorities are not the same between the HTML Page and the Construct2 source code.

Cheers !
B
3
Posts: 2
Reputation: 179

Post » Tue Mar 01, 2016 3:05 pm

Local storage is asynchronous and relies on indexedDB as well.
Also, local storage infos are not shared between browsers and domains.
So unless your script is hosted on the very same domain your game is, C2's local storage is likely not going to be compatible with other Local storage sources.
And even being hosted on the same domain, it probably is not something that will happen.

Check the How do I FAQ at the AJAX category to know how to exchange datas between a C2 client application and an external script. There is also an item on how to pass variables from the HTML page to the application IIRC.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
296
S
119
G
96
Posts: 7,332
Reputation: 71,306

Post » Tue Mar 01, 2016 3:41 pm

I need to do the opposite (pass information from construct 2 to an external web page) and informations don't need to be secure and will be cleared if user goes back to the Construct2 game page. So if I can avoid a server web page and use only static html/javascript page, it could be great. :D

I've found two ways to do this :
    Local storage
    GET parameters

With GET parameters it works but all data are stored in the URL which is not convenient for the user. But i'm pretty sure this is the best compatible solution for every browsers. I was not sure if Local Storage is a good solution (that's why i've asked if we can use local forage safely). If you say it could be dangerous, i'll go back to the GET solution (or another best solution if you have).

Thanks for the answer BTW.
B
3
Posts: 2
Reputation: 179


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 23 guests