homepage Welcome to WebmasterWorld Guest from 54.226.213.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Browsers / Google Chrome Browser
Forum Library, Charter, Moderator: open

Google Chrome Browser Forum

    
Chrome javascript issue: No variable scope from IFRAME to parent frame
Basic javascript funtion not working as it should
Peter_Dow




msg:4689260
 2:35 pm on Jul 20, 2014 (gmt 0)

I've posted some of this on the official Google chrome help forum [productforums.google.com] but since there is already an old (closed) topic in this forum about this addressing variables set inside an IFRAME [webmasterworld.com], I thought I'd join up here to add a comment on the end of that old topic about Chrome not working as it should, but when I joined, I then saw that the old topic had been closed so instead I decided to start this new topic.


First, a by-the way as to why I am using portable Chrome. I'm running Chrome portable Version 22.0.1229.94 on Windows XP - portable allows me to use a non-system directory to install the files onto and for the webpage cache thus saving system disc space which is running out.

The standard version of Chrome didn't allow me to choose and insisted on installing itself on the system disc.

Also by the way, yes I know that's an old version and someone will suggest installing the latest version to see if that fixes the error but anyway here is the issue that I want a solution to.

Have you checked our known issues page? YES - DIDN'T SEE IT.

If your issue is not there, please provide a detailed description here:

Javascript doesn't work as it should, as it does for example even on Internet Explorer 7 which I also use.

Specifically, the javascript which allows you to address variables between frames, say between a web page and an embedded IFRAME webpage, doesn't work as it should.

Here is couple of test files that demonstrate the failure of Chrome.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>DOC1.HTM</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor=pink>

<h1>DOC1.HTM</h1>

<iframe name="iframename" src="doc2.htm"></iframe>
<p>
<br />
<a href="#" onclick="alert('The value of the variable test_var set by a script in DOC2.HTM embedded in the DOC1.HTM page by an iframe named IFRAMENAME as read from a script running in DOC1.HTM appears to be - ' + window.iframename.test_var);return false;">check variable</a>
</p>

</body>
</html>




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>DOC2.HTM</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor=red>

<script type="text/javascript">

var test_var="testing, testing . . .";

</script>


<h1>DOC2.HTM</h1>
</body>
</html>




So run that in Chrome and the variable set in DOC2.HTM remains undefined - it's out of scope.

Run it in Internet Explorer and the variable is set, no problem.

The same issue arises trying to read variables the other way by using parent.variable_name from the embedded frane trying to address variables in the parent file.



The only info I get from Chrome, is a lame, worthless excuse in the Console, thus.



Unsafe JavaScript attempt to access frame with URL ... ... /doc2.htm from frame with URL ../doc1.htm. Domains, protocols and ports must match.




I've edited out the folder names from the console message for clarity.

Given the extensive javascript support of Chrome compared to the primitive offerings of IE7 in that respect, I would have expect better from Chrome, that basic functions of Javascript would work.

So what's up Chrome?

 

not2easy




msg:4689265
 3:32 pm on Jul 20, 2014 (gmt 0)

Does it change anything if you move your </head> tag down to after the </script> tag in DOC2.HTM?

Peter_Dow




msg:4689272
 3:50 pm on Jul 20, 2014 (gmt 0)

Does it change anything if you move your </head> tag down to after the </script> tag in DOC2.HTM?


No.

Wherever the script is put, head or body, it doesn't work in my Chrome browser but works fine with Internet Explorer 7.

Peter_Dow




msg:4689273
 3:52 pm on Jul 20, 2014 (gmt 0)

Can anyone get it to work with their version of Chrome?

Fotiman




msg:4689329
 1:28 am on Jul 21, 2014 (gmt 0)

Does this work?

window.iframename.contentWindow.test_var

lucy24




msg:4689333
 2:41 am on Jul 21, 2014 (gmt 0)

Wherever the script is put, head or body

Double-checking here: Are you testing everything on the same live site? The "Domains, protocols and ports must match" business reminds me of a rare-but-legitimate situation I once met where javascript only worked as intended on live links, and wouldn't work offline.

Peter_Dow




msg:4689340
 3:16 am on Jul 21, 2014 (gmt 0)

Does this work?

window.iframename.contentWindow.test_var

No that causes an error in all browsers I've tried "window.iframename.contentWindow" itself is undefined.

Neither is that the recommended way of using "contentWindow".


This is how to use contentWindow to do the same thing and again, like the earlier way, it works in Internet Explorer and Firefox, but this does NOT work in Chrome either.




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>DOC1.HTM</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor=pink>

<h1>DOC1_B.HTM</h1>

<iframe name="iframename" id="iframeid" src="doc2.htm"></iframe>
<p>
<br />
<a href="#" onclick="alert('The value of the variable test_var set by a script in DOC2.HTM embedded in the DOC1.HTM page by an iframe named IFRAMENAME as read from a script running in DOC1_B.HTM appears to be - ' + document.getElementById('iframeid').contentWindow.test_var);return false;">check variable</a>
</p>

</body>
</html>





Thanks but I am not really fishing for untested suggestions for things for me try when the person suggesting it hasn't even tried it him or herself to check it works in Chrome first.

Rather I do want help from someone who has tried everything they can think of themselves using their own Chrome browser until such time as they either

a) Post something here which works, or,
b) Post a statement here saying "Everything I have tried with my version of Chrome doesn't work. I agree it looks like an issue with Chrome."


If one person posts a) then we have a solution.

If lots of people post b) then there might arise a world web view that there is a fundamental flaw with Chrome which Google ought to put their developers onto as a priority for the next version out.

It is all very well Google adding lots of bells and whistles on to their new Chrome versions but the basic functions like javascript variable scoping between IFRAMES and should have been sorted out first.

It looks to me like Google is trying to run with Chrome before Chrome has learned to walk.

This should have been sorted out in Chrome beta or version 1 or 2 when it came out in 2008/9 because the versions of IE before then such as IE7 which came out in 2006 manage to do this no problem.

Peter_Dow




msg:4689342
 3:30 am on Jul 21, 2014 (gmt 0)


Wherever the script is put, head or body


Double-checking here: Are you testing everything on the same live site?

No. This is being tested off line and files doc1.htm and doc2.htm are in the same folder on my PC.

The "Domains, protocols and ports must match" business reminds me of a rare-but-legitimate situation I once met where javascript only worked as intended on live links, and wouldn't work offline.

OK your hunch proved correct. Well done.

Moving the doc1.htm & doc2.htm files to a folder where I test my server side php programs, which runs using Windows Internet Services Manager means I can address the files using [localhost...] addresses and bingo, Chrome behaves as it should have behaved in offline mode.

I'm not sure why you think it is "legitimate" for Chrome's javascript not to be able to directly address files in the same offline folder?

There's absolutely no security issue when you are running your own javascript files on your own PC. Or if Chrome wanted to offer a security setting that allowed or disallowed offline IFRAME variable scoping then OK, that would be fine.

But the error message is not at all clear I submit and you did well to figure out what it meant.

If Internet Explorer and Firefox etc allow you to test your javascript offline then why not Chrome?

So well done you. Not so well done Chrome.

lucy24




msg:4689353
 5:46 am on Jul 21, 2014 (gmt 0)

"Legitimate" here means that it was a recognized and accepted behavior in javascript, rather than, say, ahem, to take an example wholly at random, a bug in MSIE.

In case anyone wondered: The issue I remembered involved getImageData followed by putImageData in an HTML5 canvas. Yes, the same canvas for both. There's a quirky security issue that causes this not to work if you're not on a server (localhost will do). I stress "quirky", because how many security problems can there be if you're getting and putting the content to/from the same place, and it's your own ### computer? Go figure.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Browsers / Google Chrome Browser
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved