最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Share variables between html pages - Stack Overflow

programmeradmin5浏览0评论

In first, I know this topic is yet in StackOverflow, but I don't finish to understand how it works exactly reading the existing answers. So, I'm sorry, I hope understand it now.

I don't know if my appoach is incorrect and why it would be incorrect.

I've an html page that acts like a "visor" of another few ones, so from this "main" page, I can watch the other ones embedding them in an area of the visor page (with iframe).

At the same time, I have a mon .js file for all the pages (including visor).

So for example, if I have a variable declared with a valor of "hello world" (string type) in .js, I am able to access this variable from visor, or embedded page, and still haves its original valor. But if I change its valor from visor (or embedded), the other page is unable to read the new valor changed from the other page.

Its possible make a global-between-pages variable?.

I make a try with localStorage, but it only works for the particular page where it is used.

So, let's see, more or less:

.JS FILE

var Example = "Hello world";

function TellMe()
{
    alert(Example);
}

function ChangeVar()
{
    Example = "Goodbye world";
}

PAGE 1 (with referenced .js file in the head section, of course)

<div onclick="ChangeVar();">click!</div>

PAGE 2 (with referenced .js file in the head section, of course), but AFTER div of page 1 have been clicked

<div onclick="TellMe();">click!</div>

this would alert "Hello World", in place of "Goodbye World".

So I understand every page haves its own version of .js variables, even when all are using the same .js, and even when one html in being accessed from another one (embedded with iframe).

How can I get this example resulting after click in the div of page 2 with the message "Goodbye World"?.


I'll be more explanatory, seeing the amount of answers telling about hacks, waste other pages, and inflate bank accounts.

It's just about a documentation page (from a software), I have a "visor" page, and from it, I can access the other ones pages. Actually I have "visor" page in the root folder, and the other pages, inside a folder in this root directory, all would be in the same domain, isn't it?.

I want "next" and "previus" buttons on pages, for avoid the user back to visor and choose the new page, but I need to know the number of the page the user is watching for know what page show at next or previus.

In first, I know this topic is yet in StackOverflow, but I don't finish to understand how it works exactly reading the existing answers. So, I'm sorry, I hope understand it now.

I don't know if my appoach is incorrect and why it would be incorrect.

I've an html page that acts like a "visor" of another few ones, so from this "main" page, I can watch the other ones embedding them in an area of the visor page (with iframe).

At the same time, I have a mon .js file for all the pages (including visor).

So for example, if I have a variable declared with a valor of "hello world" (string type) in .js, I am able to access this variable from visor, or embedded page, and still haves its original valor. But if I change its valor from visor (or embedded), the other page is unable to read the new valor changed from the other page.

Its possible make a global-between-pages variable?.

I make a try with localStorage, but it only works for the particular page where it is used.

So, let's see, more or less:

.JS FILE

var Example = "Hello world";

function TellMe()
{
    alert(Example);
}

function ChangeVar()
{
    Example = "Goodbye world";
}

PAGE 1 (with referenced .js file in the head section, of course)

<div onclick="ChangeVar();">click!</div>

PAGE 2 (with referenced .js file in the head section, of course), but AFTER div of page 1 have been clicked

<div onclick="TellMe();">click!</div>

this would alert "Hello World", in place of "Goodbye World".

So I understand every page haves its own version of .js variables, even when all are using the same .js, and even when one html in being accessed from another one (embedded with iframe).

How can I get this example resulting after click in the div of page 2 with the message "Goodbye World"?.


I'll be more explanatory, seeing the amount of answers telling about hacks, waste other pages, and inflate bank accounts.

It's just about a documentation page (from a software), I have a "visor" page, and from it, I can access the other ones pages. Actually I have "visor" page in the root folder, and the other pages, inside a folder in this root directory, all would be in the same domain, isn't it?.

I want "next" and "previus" buttons on pages, for avoid the user back to visor and choose the new page, but I need to know the number of the page the user is watching for know what page show at next or previus.

Share Improve this question edited Jan 15, 2014 at 4:02 Reaversword asked Jan 15, 2014 at 3:38 ReaverswordReaversword 1671 gold badge4 silver badges11 bronze badges 7
  • 1 Your choice of tags of this question ("html variables between pages") was unlikely to help you. However, "html variables" may be sufficient. (Which is to say "between" is not a good topic name.) – Richard Commented Jan 15, 2014 at 3:42
  • 1 Ok richard, thanks for the point. – Reaversword Commented Jan 15, 2014 at 3:44
  • 1 localStorage can persist across multiple pages (if it doesn't work then you're doing it wrong): "All the pages that e from your domain share the same storage area, so you can use this mechanism to keep data persistent between multiple pages. The data also stays on the client machine (until you remove it), so it can be used to keep track of information over time." – user2864740 Commented Jan 15, 2014 at 3:44
  • 1 (I'm not advocating that localStorage is a "good" approach here, but it will work in supporting environments.) – user2864740 Commented Jan 15, 2014 at 3:49
  • 2 Possible duplicate of Share data between html pages – Muhammad Usman Commented Feb 4, 2017 at 9:13
 |  Show 2 more ments

5 Answers 5

Reset to default 6

I am sharing you the very simple example on localstorage that should fix your requirement. Consider you have two file page1.html and page2.html.

Inside of page1.html add following lines

<div id="notes">
</div>

<script>
if(typeof(Storage)!=="undefined")
  {
  localStorage.lastname="Smith";
  document.getElementById("notes").innerHTML="Last name: " + localStorage.lastname;
  }
else
  {
  document.getElementById("notes").innerHTML="Sorry, your browser does not support web storage...";
  }
</script>

Inside of page2.html

<div id="notes">
</div>

<script>
if(typeof(Storage)!=="undefined" && localStorage.lastname)
  {
  document.getElementById("notes").innerHTML="Last name: " + localStorage.lastname;
  }
else
  {
  document.getElementById("notes").innerHTML="Sorry, your browser does not support web storage...";
  }
</script>

You can access values from localstorage in any page now.

Another option

You can pass values from one page to another through querystring parameter. For eg. <a href='page2.html?index=someValue'>goto next page</a> Now in page2.html you can fetch this index value and set the required field, all through javascript.

Hope that answers your query.

HTML Local Storage should be what you are looking for . Here is a link to know more - http://diveintohtml5.info/storage.html

Also you could use window.name known as JavaScript session. But it only works as long as the same window/tab is used. Link - http://www.thomasfrank.se/sessionvars.html

Plus there are cookies obviously.

My order of preference would be the order in which I have stated my solutions.

The problem here is a security concern with Javascript.

Even doing simple event such as jQuery .load() the additional scripts on the page won't load, due to CORS issues.

Variables are not passable among pages, especially with iframes, as there could be all sorts of malicious activity going on.

My advice is to load the same JS doc within each iframe you have going on (if you're able to do so.).

You will not be able to load js files into sites that are not on your domain.

Yes, basically, all of you are right.

It's enough with just store the variable you want manipulate between pages inside a localStorage.variableToManipulate. Directly if we want.

So is enough with something like (I'll repeat my first example corrected):

.JS FILE

localStorage["Example"] = "Hello world";

function TellMe()
{
    alert(localStorage["Example"]);
}

function ChangeVar()
{
    localStorage["Example"] = "Goodbye world";
}

PAGE 1 (with referenced .js file in the head section, of course)

<div onclick="ChangeVar();">click!</div>

PAGE 2 (with referenced .js file in the head section, of course), but AFTER div of page 1 have been clicked

<div onclick="TellMe();">click!</div>

BUT, just a thing, when you make this kind of test, remember to have opened just ONE html page. I was testing with the two ones opened (Visor and PageN), and I think every one was using its own instance of the localStorage variable (modified by the own page itself).

Just a rookie stupid error!. I'm sorry, and many thanks for be there lending a hand!.

I know it is late but whoever wants to share data between pages under the same domain they can utilize SharedWorker

发布评论

评论列表(0)

  1. 暂无评论