LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-02-2021, 08:50 AM   #1
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,289

Rep: Reputation: 52
javascript localStorage not working


I have a CGI script storing value in localStorage and an HTML page (Firefox and Javascript in both cases) trying to read the value but not finding it (key is 'null'). It seems I do not understand the "same origin" correctly.
This is done on a home testing PC and the only difference is that the CGI is located in /usr/lib/cgi-bin/ while the html page is located in /var/www/html/ on the same machine. The CGI displays the value stored in localStorage when checked.
According to my understanding, the different paths do not change the "origin".

Has anyone any suggestion of what to look for (except typos)?

Thank you for your help.
 
Old 05-04-2021, 04:37 AM   #2
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware, Slarm64 & Android
Posts: 16,339

Rep: Reputation: 2331Reputation: 2331Reputation: 2331Reputation: 2331Reputation: 2331Reputation: 2331Reputation: 2331Reputation: 2331Reputation: 2331Reputation: 2331Reputation: 2331
Nobody can help you much on that info.
  • Where is local storage?
  • What user is the script running as?
  • What is happening, and what do you want to happen?
  • What error shows and have you searched for it?
  • Post also full details of distro and all packages/libraries involved.
 
1 members found this post helpful.
Old 05-04-2021, 08:25 AM   #3
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,289

Original Poster
Rep: Reputation: 52
Thank you for the answer.
I had to go on and solved the problem by having the two HTML pages created by a CGI script, now I can find the value saved on client's localStorage from both HTML pages. I suspect my knowledge is insufficient to understand the solution as the problem was probably due to HTTP headers and likely some adjustments in Apache2 of which I do not know enough - too complicated for the objective which is a simple demo I am unlikely to have to repeat.
No error was showing.

Last edited by rblampain; 05-04-2021 at 08:38 AM.
 
Old 05-04-2021, 08:46 AM   #4
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,612

Rep: Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553
Quote:
Originally Posted by rblampain View Post
I have a CGI script storing value in localStorage
No, you don't. LocalStorage is client-side data - a server-side script does not have access to it.

Your script may be generating a HTML/JS page which is sent to the browser, containing code which interacts with LocalStorage, but the CGI script itself is not storing any value.

Any issue you have in storing/retrieving values is based on something other than whether the page is generated or a static file (because the client doesn't know or care about that).


Quote:
It seems I do not understand the "same origin" correctly.
See https://developer.mozilla.org/en-US/docs/Glossary/Origin


Quote:
Originally Posted by rblampain View Post
I suspect my knowledge is insufficient to understand the solution as the problem was probably due to HTTP headers and likely some adjustments in Apache2 of which I do not know enough - too complicated for the objective which is a simple demo I am unlikely to have to repeat.
Again, local storage is not server or HTTP based. You've not posted any URLs or code, so nobody can confirm why it wasn't working as expected.

There are working examples at https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API

The MDN docs are usually a reasonable place to start when you want to learn about something web/browser related.


Last edited by boughtonp; 05-04-2021 at 08:49 AM.
 
1 members found this post helpful.
Old 05-09-2021, 12:09 AM   #5
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,289

Original Poster
Rep: Reputation: 52
Quote:
Your script may be generating a HTML/JS page which is sent to the browser, containing code which interacts with LocalStorage, but the CGI script itself is not storing any value.
Correct. This is the case and I have not explained myself clearly.
There is say Page1.html (as seen by the server as 'index.html') containing a Form calling "make_page2.cgi" which generates and returns Page2.html.
The same Javascript code (copied and pasted) in both Page1.html and Page2.html gives different results with one finding the key/value pair in localStorage while the other finds it is null.
My question was: is this to be expected due to the differences of transport to the client? If the answer is no (as all info on the subject seems to suggests) then I need to dig further to find the cause.
Unfortunately, as I previously explained, this digging is not justified although other readers may want to learn something from this. I was expecting to become aware of a simple 'gotcha' while there may be none.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: JavaScript creator Eich's latest project: KILL JAVASCRIPT LXer Syndicated Linux News 0 06-18-2015 08:30 AM
Looking for Textbrowser with JavaScript Support or Elink Javascript Support rohezal Linux - Software 4 09-01-2009 01:02 PM
Ubuntu Edgy/Firefox 2/Javascript - Firefox closes accessing websites with Javascript Interdictor Ubuntu 8 11-02-2006 11:58 AM
Javascript from AdSense not working properly linux-rulz General 2 03-29-2005 11:25 PM
Javascript not working in mozilla 1.6 ..Plz Help Me its_m3 Linux - Software 0 07-29-2004 04:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration