1

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

2
  • You cannot use cookies to solve this problem. See code.google.com/p/chromium/issues/detail?id=535 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. Commented Jun 5, 2011 at 0:10

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Thanks for both answers, they are both a big help. I'm going to go with Vincent's, since it seems everything should support it. I wish I had more points so I could give points :-)
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.