javascript - window.parent.location.href IE9 Bug? - Stack Overflow

Scenario:domain1 hosts an iFrame of domain2.domain2 is using javascript to trigger dynamic links that

Scenario:

  1. domain1 hosts an iFrame of domain2.

  2. domain2 is using javascript to trigger dynamic links that need to route to the parent window.

  3. The links are all relative paths.

  4. Clicking on /linkA.html (in iframe on domain2) routes the parent to domain1/linkA.html.

    var _generic = "/linkA.html";

        $("#canvas").each( function () {
            $(this).children("a").attr("href",_generic);
            $(this).click( function(e) {
                e.preventDefault();
                window.parent.location.href = _generic;
            } );
        } );
    

Changing the links to absolute (domain2/linkA.html) solves the functional problem.

Has anyone run into this before?

Scenario:

  1. domain1. hosts an iFrame of domain2..

  2. domain2. is using javascript to trigger dynamic links that need to route to the parent window.

  3. The links are all relative paths.

  4. Clicking on /linkA.html (in iframe on domain2.) routes the parent to domain1./linkA.html.

    var _generic = "/linkA.html";

        $("#canvas").each( function () {
            $(this).children("a").attr("href",_generic);
            $(this).click( function(e) {
                e.preventDefault();
                window.parent.location.href = _generic;
            } );
        } );
    

Changing the links to absolute (domain2./linkA.html) solves the functional problem.

Has anyone run into this before?

Share Improve this question asked Jul 13, 2011 at 19:21 tgormtxtgormtx 3221 gold badge2 silver badges9 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 1

In order for the browser to resolve the entire url when setting relative paths, it first needs to read the current href, and this is blocked by the SOP.

But I am pretty sure that if you use parent.location instead of parent.location.href, then this will work just fine.

You should try using window.opener.location instead of window.parent.location.

I have seen a difference in IE9 vs. IE8, FireFox, and Webkit. Consider the following code:

Parent source:

<!DOCTYPE html>
<html>
<head><title>iframe test page</title></head>
<body>
    <h1>Parent window</h1>
    <iframe src="//domain2/iframe.html"></iframe>
</body>
</html>

iframe source (on separate domain):

<!DOCTYPE html>
<html>
<head><title>iframe content</title></head>
<body>
    <h1>iframe content</h1>
    <script>function redirect() { window.parent.location = "/new-href"; } </script>
    <a href="javascript:redirect()">Redirect the parent window please</a>
</body>
</html>

If the main page is opened in IE8 (or IE9 set to run in IE8 mode or Chrome 23 or FF16) clicking the link results in navigating to //(iframe domain)/new-href whereas in IE9 it goes to //(original parent domain)/new-href

I am still investigating how to work around this issue and will update this if/when I have more information.

UPDATE: Setting the target="_top" attribute for iframe links works around this problem.

Updated iframe source (on separate domain):

<!DOCTYPE html>
<html>
<head><title>iframe content</title></head>
<body>
    <h1>iframe content</h1>
    <a href="/new-href" target="_top">Redirect the parent window please</a>
</body>
</html>

Clicking the link in this updated iframe code will redirect the parent (entire) window to //(iframe domain)/new-href in both IE8 & IE9 (and, as far as I know, all browsers as well). That is what you want to do, right?

e.g. If you want to make all tags in an iframe cause the top/main/parent window to navigate somewhere rather than the iframe navigating somewhere then use something like this:

$("iframe a").attr("target", "_top");

See also target attribute documentation

The forward slash implicit folder in the URL works in Moz. browsers, but with explorer, links from the same folder don not put the forward slash. It's simply X-browser syntax. You may have done better puting ./ (dot forward slash with explorer but the proper way is no slash preceding the filename for a same folder URL).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745364448a4624511.html

相关推荐

  • javascript - window.parent.location.href IE9 Bug? - Stack Overflow

    Scenario:domain1 hosts an iFrame of domain2.domain2 is using javascript to trigger dynamic links that

    19小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信