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

Accessing javascript variables in different frames in chrome - Stack Overflow

programmeradmin0浏览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

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

Share Improve this question edited Feb 11, 2011 at 22:29 russell asked Feb 11, 2011 at 22:20 russellrussell 7061 gold badge10 silver badges19 bronze badges 2
  • You cannot use cookies to solve this problem. See code.google./p/chromium/issues/detail?id=535 – Mark Eirich Commented Feb 11, 2011 at 23:50
  • Fyi, javascript: does not belong into an onclick attribute. The only reason it's not a syntax error is the fact that something: defines a label and is pretty much a no-op in your case. – ThiefMaster Commented Jun 5, 2011 at 0:10
Add a ment  | 

3 Answers 3

Reset to default 2

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.

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.

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

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

发布评论

评论列表(0)

  1. 暂无评论