Javascript window.location 在 IE7/8 中无法正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4818868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:16:04  来源:igfitidea点击:

window.location not working correctly in IE7/8

javascriptjqueryxhtml

提问by Gregory Bolkenstijn

I have a div which has an a inside it. I want to click on the div and follow the same url as the a. This is the HTML:

我有一个里面有 a 的 div。我想点击 div 并遵循与 a. 这是 HTML:

<div class="overview white getchildurl">
    <p class="w70">
    05-02-2011
    </p>
    <a class="button" href="details/">Details</a>
    <span class="clear"></span>
</div>

and I have this script:

我有这个脚本:

$('.getchildurl').each(function() {     
    $(this).click(function() {
        var href = $(this).children('a').first().attr('href');
        window.location = href;
    });
});

This works in Firefox 3.6, Chrome, Safari, but not in IE7 and IE8. It reloads the page, but stays on the same page. I checked the var href, it has the right url, but doesn't go there. What am I doing wrong?

这适用于 Firefox 3.6、Chrome、Safari,但不适用于 IE7 和 IE8。它重新加载页面,但保持在同一页面上。我检查了 var href,它有正确的网址,但没有去那里。我究竟做错了什么?

Thanks.

谢谢。

EDIT: Making it an absolute URL made it work. This is the new script:

编辑:使其成为绝对 URL 使其工作。这是新脚本:

$('.getchildurl').each(function() {     
    $(this).click(function() {
        var href = $(this).children('a').first().attr('href');
        var host = window.location.hostname;
        window.location = 'http://' + host + '/' + href;
    });
});

采纳答案by Bryan Field

I am posting this as a different answer because it is a different answer. How to work through problem with JavaScript instead of work through it with HTML and CSS.

我将此作为不同的答案发布,因为它是一个不同的答案。如何使用 JavaScript 解决问题,而不是使用 HTML 和 CSS 解决问题。

Try changing your JavaScript to display and make sure the values are what you think they are

尝试更改您的 JavaScript 以显示并确保值是您认为的那样

$(this).click(function() {
    var href = $(this).children('a').first().attr('href');
    alert(href)
    alert(href.length)
    window.location = href;
});

That will display the hrefand the hreflength. Make sure the length matches up. If the length is too big then that means there are hidden characters.

这将显示长度hrefhref长度。确保长度匹配。如果长度太大,则意味着存在隐藏字符。

If you determine that the problem is due to syntax, many URL fixes/conversions can be done with JavaScript. For example, removing a trailing slash when there is one can be done with

如果您确定问题是由语法引起的,可以使用 JavaScript 完成许多 URL 修复/转换。例如,可以在有斜杠时删除尾部斜杠

$(this).click(function() {
    var href = $(this).children('a').first().attr('href');
    window.location = href.replace(/\/$/, '');
});

Also, the URL could be converted to an absolute URL using JavaScript if that is what will fix the problem. And any other troublesome or even invisible characters can be either removed or replaced.

此外,如果这样可以解决问题,可以使用 JavaScript 将 URL 转换为绝对 URL。任何其他麻烦甚至不可见的字符都可以删除或替换。

Can you post a web pageon the internet that we can access to see the issue on our own copies of IE? Can you post an HTML file that we can copy to our machine and view in a browser? Your comment on the other question tells me you have some sort of XHTML strict DOCTYPE. If one of us can make the issue happen on our end, it is more likely to be fixed.

您能否在 Internet 上发布一个网页,我们可以访问该网页以查看我们自己的 IE 副本上的问题?你能发布一个 HTML 文件,我们可以复制到我们的机器上并在浏览器中查看吗?你对另一个问题的评论告诉我你有某种 XHTML 严格的 DOCTYPE。如果我们中的一个人可以让问题发生在我们这边,那么它就更有可能得到解决。

回答by jAndy

As I mentioned in my comment, I'm not 100% sure about it (how IE7+8 handle the .locationobject), but after all it's just an object.

正如我在评论中提到的,我不是 100% 确定(IE7+8 如何处理.location对象),但毕竟它只是一个对象。

Try to explicitly set the hrefproperty within the locationobject:

尝试hreflocation对象内显式设置属性:

window.location.href = href;

回答by Peter Ilfrich

Although the topic is old, it's not obsolete. I just had the same problem.

虽然这个话题很老,但它并没有过时。我只是遇到了同样的问题。

window.location = href;

works fine, if you put another command after that.

工作正常,如果您在此之后放置另一个命令。

window.location = href;
return false;

This works for me and redirects with the correct URL. Hope this helps others with the same problem.

这对我有用并使用正确的 URL 重定向。希望这可以帮助其他有同样问题的人。

回答by Bryan Field

Not exactly a direct answer to you question but I have two suggestions

不完全是对您问题的直接回答,但我有两个建议

  1. You may just want to try to see if jQuery will handle it for you

    $(this).children('a').first().click()
    

    Edit: This did not solve the problem

  2. Also you may just want to make the whole <divin to an <atag with style="display: block; text-decoration: none". You can use font colors and underlines to make the text inside look however you want. Also, this solution does not require JavaScript. and it also makes the whole div show as focused instead of just the a or nothing at all.

  1. 您可能只想尝试看看 jQuery 是否会为您处理

    $(this).children('a').first().click()
    

    编辑:这并没有解决问题

  2. 你也可能只是想使整个<div在一个<a与标签style="display: block; text-decoration: none"。您可以使用字体颜色和下划线使里面的文本看起来像您想要的那样。此外,此解决方案不需要 JavaScript。并且它还使整个 div 显示为重点,而不仅仅是 a 或根本没有。