IE与Chrome中window.location.href和<base>标签的区别

3

我在使用JavaScript时,希望通过点击一个来将页面重定向到不同的页面,但遇到了问题。我的网站中使用标签作为链接的起始点。

请考虑以下示例代码:

<!doctype html>
<html lang="en"><head>
    <meta charset="utf-8"/>
<title>Login</title>
<base href="http://www.mysite.com/MyWebsite/manage/" />

特别提到 <base href="http://www.mysite.com/MyWebsite/manage/" />

并考虑以下jQuery代码:

//Set row clicking
    $('table#alerts_table').on("click", "td", function () {
        var alert_id = $(this).closest('tr').find('input[type=checkbox]').val();
        var href = 'alerts/alert.php?id=' + alert_id;
        if (href) { window.location.href = href; }
    });

特别提到
var href = 'alerts/alert.php?id=' + alert_id;
if (href) { window.location.href = href; }

请注意,当前我在网站上的位置已经位于名为"alerts"的文件夹中,如下所示: http://www.mysite.com/MyWebsite/manage/alerts/index.php。

我想从index.php移动到alert.php。

在Chrome浏览器中,所有内容都按照预期工作,即将基本href http://www.mysite.com/MyWebsite/manage/ 连接到alerts/alert.php?id=1,并且我会被相应地重定向。

然而,在IE浏览器中, 文件夹"alerts"被重复了: http://www.mysite.com/MyWebsite/manage/alerts/alerts/alert.php?id=1

这意味着它并没有从基本标记中获取基本URL,而是使用相对路径。

有任何想法吗?

1个回答

0

你可以使用:

   document.location.replace(); 

函数。您不需要使用base选项。它将重定向到位于相同目录中的页面或子目录。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接