简体   繁体   English

在 html 页面之间共享变量

[英]Share variables between html pages

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.首先,我知道这个主题还在 StackOverflow 中,但我还没有完全理解它是如何工作的,阅读现有的答案。 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).我有一个 html 页面,它就像其他几个页面的“遮阳板”,所以从这个“主”页面,我可以看到其他页面将它们嵌入到遮阳板页面的区域(使用 iframe)。

At the same time, I have a common .js file for all the pages (including visor).同时,我对所有页面(包括遮阳板)都有一个通用的 .js 文件。

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.因此,例如,如果我在 .js 中有一个声明为“hello world”(字符串类型)的变量,我可以从 visor 或嵌入页面访问该变量,并且仍然具有其原始值。 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.我尝试使用 localStorage,但它仅适用于使用它的特定页面。

So, let's see, more or less:所以,让我们或多或少地看看:

.JS FILE .JS文件

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)第 1 页(当然,在 head 部分中引用了 .js 文件)

<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第 2 页(当然,在 head 部分中引用了 .js 文件),但是在单击了第 1 页的 div 之后

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

this would alert "Hello World", in place of "Goodbye World".这将提醒“Hello World”,而不是“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).所以我理解每个页面都有自己的 .js 变量版本,即使所有页面都使用相同的 .js,甚至当一个 html 被另一个(嵌入 iframe)访问时。

How can I get this example resulting after click in the div of page 2 with the message "Goodbye World"?.单击带有消息“再见世界”的第 2 页的 div 后,如何获得此示例?。


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?.实际上,我在根文件夹中有“visor”页面,而在此根目录中的文件夹中的其他页面都在同一个域中,不是吗?

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.我想要页面上的“下一个”和“上一个”按钮,以避免用户返回遮阳板并选择新页面,但我需要知道用户正在观看的页面编号,了解下一个或上一个显示的页面。

I am sharing you the very simple example on localstorage that should fix your requirement.我正在与您分享有关 localstorage 的非常简单的示例,该示例应该可以满足您的要求。 Consider you have two file page1.html and page2.html.假设您有两个文件 page1.html 和 page2.html。

Inside of page1.html add following lines在 page1.html 中添加以下几行

<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 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.您现在可以在任何页面中访问来自 localstorage 的值。

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. <a href='page2.html?index=someValue'>goto next page</a>现在在 page2.html 中,您可以通过 javascript 获取此索引值并设置所需字段。

Hope that answers your query.希望能解答您的疑问。

HTML Local Storage should be what you are looking for . HTML 本地存储应该是您正在寻找的。 Here is a link to know more - http://diveintohtml5.info/storage.html这是了解更多信息的链接 - http://diveintohtml5.info/storage.html

Also you could use window.name known as JavaScript session.您也可以使用window.name称为 JavaScript 会话。 But it only works as long as the same window/tab is used.但它只有在使用相同的窗口/选项卡时才有效。 Link - http://www.thomasfrank.se/sessionvars.html链接 - 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 .这里的问题是Javascript的安全问题。

Even doing simple event such as jQuery .load() the additional scripts on the page won't load, due to CORS issues.由于CORS问题,即使执行jQuery .load()等简单事件,页面上的其他脚本也不会加载。

Variables are not passable among pages, especially with iframes , as there could be all sorts of malicious activity going on. Variables在页面之间是不可传递的,尤其是iframes ,因为可能存在各种恶意活动。

My advice is to load the same JS doc within each iframe you have going on (if you're able to do so.).我的建议是在您正在进行的每个iframe加载相同的JS文档(如果您能够这样做)。

You will not be able to load js files into sites that are not on your domain.您将无法将js文件load到不在您的域中的站点。

Yes, basically, all of you are right.是的,基本上,你们都是对的。

It's enough with just store the variable you want manipulate between pages inside a localStorage.variableToManipulate.只需在 localStorage.variableToManipulate 中存储您想要在页面之间操作的变量就足够了。 Directly if we want.如果我们想要,直接。

So is enough with something like (I'll repeat my first example corrected):这样就足够了(我将重复我更正的第一个示例):

.JS FILE .JS文件

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)第 1 页(当然,在 head 部分中引用了 .js 文件)

<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第 2 页(当然,在 head 部分中引用了 .js 文件),但是在单击了第 1 页的 div 之后

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

BUT, just a thing, when you make this kind of test, remember to have opened just ONE html page.但是,只是一件事,当您进行此类测试时,请记住只打开一个 html 页面。 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).我正在测试打开的两个(Visor 和 PageN),我认为每个人都使用自己的 localStorage 变量实例(由自己的页面本身修改)。

Just a rookie stupid error!.只是一个菜鸟愚蠢的错误!。 I'm sorry, and many thanks for be there lending a hand!.对不起,非常感谢您的帮助!

我知道现在已经晚了,但是无论谁想在同一域下的页面之间共享数据,他们都可以使用SharedWorker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM