我没有足够的分数来评论埃文的回答来建议更正,所以我所能做的就是在此处发布更正。简而言之,document.createElement(a)
缺少引号,应该document.createElement("a")
代替。这也应该解决凯文对 FF5 的担忧。
我写的整个函数:
function goTo(url)
{
var a = document.createElement("a");
if (a.click)
{
// HTML5 browsers and IE support click() on <a>, early FF does not.
a.setAttribute("href", url);
a.style.display = "none";
document.body.appendChild(a);
a.click();
} else {
// Early FF can, however, use this usual method
// where IE cannot with secure links.
window.location = url;
}
}
这适用于我们使用 IE7、IE8、FF3、FF7 和 Chrome 的 HTTPS 环境。所以我想它也适用于FF5。如果没有这种解决方法,我们在尝试设置 window.location 时会在 IE7 和 IE8 中出现 403 错误。至于沙乐问IE为什么要这么做,我只能猜测他们认为太不安全了。我在 IE 中遇到了与 window.open 类似的问题,我也必须解决这个问题。