在chrome中访问不同框架中的javascript变量

发布于 2024-10-16 19:25:30 字数 847 浏览 1 评论 0原文

我在 chrome 框架之间传递 javascript 值时遇到问题。在其他浏览器(Opera 和 Firefox)中它可以工作。第一帧包含以下 html:

<script> variable="frame 1 value";</script>
<a href="" onclick="javascript:parent.frames[1].location='test.html';">click here</a>

, test.html 是:

<html>
<head>
<script>window.onload = function() {
  div = document.getElementById("fred");
  div.innerHTML="<b>" + top.frames[0].variable + "</b>";
  }
</script>
</head>
<body>
  <div id="fred">
hi there</div>
</body>
</html>

我查看过此站点和其他站点,并且看到了一个建议,因为 chrome 页面在不同的进程中运行,所以它们无法传递值。这是真的吗,如果是这样,有没有办法解决它(cookies?)

谢谢,

罗素

(编辑)我刚刚找到了另一个答案,它说这种情况只发生在文件协议上。就像另一个问题的作者一样,我正在编写一个应用程序,旨在从 CD 上运行,因此我需要使用文件协议。我使用的Chrome版本是9.0。

I am having problems passing javascript values between frames in chrome. In other browsers (Opera and Firefox) it works. The first frame contains the following html:

<script> variable="frame 1 value";</script>
<a href="" onclick="javascript:parent.frames[1].location='test.html';">click here</a>

and test.html is:

<html>
<head>
<script>window.onload = function() {
  div = document.getElementById("fred");
  div.innerHTML="<b>" + top.frames[0].variable + "</b>";
  }
</script>
</head>
<body>
  <div id="fred">
hi there</div>
</body>
</html>

I have looked on this site and others, and the have seen a suggestion that because chrome pages run in different processes they cannot pass values. Is this true, and if so is there a way around it (cookies?)

Thanks,

russell

(edited) I just found another answer which says this happens only on file protocol. Like the writer of the other question, I am writing an applicaiton meant to be run off a cd, so I need to use file protocol. The version of Chrome I am using is 9.0.

ry

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光无声 2024-10-23 19:25:30

HTML5 存储来救援!对于第一帧:

<script>localStorage.setItem('variable', 'frame 1 value');</script>
<a href="#" onclick="javascript:parent.frames[1].location='test.html';return false">click here</a>

对于 test.html:

<html><head>
    <script>
        window.onload = function() {
            div = document.getElementById("fred");
            div.innerHTML="<b>" + localStorage.getItem('variable') + "</b>";
        }
    </script>
</head><body>
    <div id="fred">hi there</div>
</body></html>

注意:IE7 和一些旧版浏览器不支持 localStorage。但是,您应该能够使用 if (typeof(localStorage) == 'undefined') {} 来检测您需要使用哪种方法。

HTML5 Storage to the rescue! For the first frame:

<script>localStorage.setItem('variable', 'frame 1 value');</script>
<a href="#" onclick="javascript:parent.frames[1].location='test.html';return false">click here</a>

And for test.html:

<html><head>
    <script>
        window.onload = function() {
            div = document.getElementById("fred");
            div.innerHTML="<b>" + localStorage.getItem('variable') + "</b>";
        }
    </script>
</head><body>
    <div id="fred">hi there</div>
</body></html>

A note of caution: IE7 and some older browsers do not support localStorage. However, you should be able to use if (typeof(localStorage) == 'undefined') {} to detect which method you need to use.

何处潇湘 2024-10-23 19:25:30

这与跨站点脚本有关,这可能是一个安全问题。由于Chrome对此有非常严格的行为,所以应该不可能实现你想要的。

幸运的是,您可能可以使用一个巧妙的技巧(如果您的变量只是一个字符串):

  1. 将第一帧中的链接更改为 test.html?foo=bar

  2. 读取第二框架中的 window.location.href 。这将产生类似“Z:\folder\test.html?foo=bar”的内容。现在,您可以使用字符串操作函数从 href 中提取 foo 的值(例如:bar)。

This has something to do with cross-site scripting which may be a security issue. Since Chrome has a very strict behavior on this, it should be impossible to achieve what you want.

Fortunately, there may be a nifty trick that you can use (if your variable is only a string):

  1. Change the link in the first frame to test.html?foo=bar

  2. Read window.location.href in the second frame. This will yield something like "Z:\folder\test.html?foo=bar". Now you can use string manipulation functions to extract the value of foo (in case: bar) from the href.

南风几经秋 2024-10-23 19:25:30

由于多种原因,自 1997 年(HTML 4.0 规范)以来,框架已被弃用 - 因此最好的建议是不要使用它们。

您还可以使用命令行参数 --disable-web-security 运行 Chrome,但这也是一个不好的建议。

Frames are deprecated since 1997 (HTML 4.0 specification) for many reasons - so the best recommendation is do not use them.

You can also run Chrome with command line argument --disable-web-security, but it is also bad recommendation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文