窗口的位置.href不会改变URL

4

我想要改变表单提交后出现的URL,然而下面的代码似乎没有实现我所需要的功能。如何在地址栏中更改URL?

代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XMHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
    <meta http-equiv="content-language" content="en-us" />
    <meta http-equiv="cache-control" content="no-cache" />
    <meta http-equiv="pragma" content="no-cache" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <meta name="author" content="" />
    <meta name="copyright" content="&copy; 2012" />
    <meta name="robots" content="noindex, nofollow" />

    <title>sample form</title>

    <link rel="stylesheet" type="text/css" media="all" href="" />

    <style type="text/css">

    </style>

    <script type="text/javascript">

        var testurl = window.location.href;
        if(testurl == "file:///C:/Users/user/Desktop/sample_form.html?q=test&submit=submit") {
            testurl = testurl.replace(/q=[^&]+&[a-z]+\=[a-z]+/,'test');
            alert(testurl);
        }

    </script>
</head>

<body>

    <form name="sampleform" id="sampleform" method="get" action="sample_form.html">
        <input type="text" name="q" value="" id="q" />
        <input type="submit" name="submit" value="submit" id="submit" />
    </form>

</body>
</html>

1
由于您定位脚本的方式,页面在加载之前将立即重定向到新 URL。 - Asad Saeeduddin
@Asad = 如果我正在检查testurl是否等于URI中的值,那么它为什么会这样做呢? - PeanutsMonkey
我是指如果您使用了KingKongFrog的解决方案。如果您在那里更改了window.location.href的值,您将在页面加载完成之前被重定向。 - Asad Saeeduddin
4个回答

3
请尝试以下操作:
<script type="text/javascript">

    var testurl = document.URL;
    if(testurl == "file:///C:/Users/user/Desktop/sample_form.html?q=test&submit=submit") {
        testurl = testurl.replace(/q=[^&]+&[a-z]+\=[a-z]+/,'test');
        window.location.href = testurl;
    }

</script>

注意:

使用window.location可以读取和写入与当前框架相关联的位置对象。如果你只想获取地址作为只读字符串,可以使用document.URL,它应该包含与window.location.href相同的值。


1
这将重定向您到testurl中设置的URL。我认为PeanutsMonkey只想更改地址栏中的文本,而不进行重定向。 - Zefiryn
@KingKongFrog - 谢谢。我能否在if语句中使用正则表达式,以便不必硬编码URI? - PeanutsMonkey
@Zefiryn - 理想情况下,是的,那就是我想做的。 - PeanutsMonkey
@KingKongFrog - 我该怎么做? - PeanutsMonkey

3
要更改URL,您必须执行以下操作:
window.location.href = testurl;

但这样做的“副作用”是用新的URL加载整个页面(无论是否相同)。
如果您想更改显示内容但不重新加载页面,则必须使用 pushState来使用新的HTML5历史记录API。这就是许多Ajax站点中所做的,以使URL反映动态加载页面中的内容,但它与IE9-不兼容。
var stateObj = { foo: "bar" };
history.pushState(stateObj, "some useless title", testurl);

很遗憾,用户正在使用<IE9。 - PeanutsMonkey
所以不要在客户端更改URL。 - Denys Séguret
我无法访问或控制服务器端,因为它是一款专有应用程序,只允许对客户端进行更改。 - PeanutsMonkey

1

简而言之: testurl 不是对 window.location.href 的引用(它们只是具有相同的值),因此您需要直接将 URL 分配给 window.location.href,而不是 testurl。例如:window.location.href = "http://example.com/redirect.html";

基本上,假设您的网站位于 example.com/index.html,并且您想要重定向到 example.com/redirect.html。

您的问题出现在您将位置分配给 testurl 而不是 window.location.href。让我解释一下:

  1. var testurl = window.location.href;window.location.href 的值(即 example.com/index.html)赋给变量 testurl。所以此时,testurlwindow.location.href 都被设置为 example.com/index.html,但它们之间没有任何关系(testurl 不是对 window.location.href 的引用,它只是具有相同的值)。
  2. testurl == testurl.replace(...); 用你想要重定向到的站点替换 testurl,但由于 testurl 不是window.location.href 的引用,因此 testurl 变成了 example.com/redirect.html,而 window.location.href 仍然是之前的值,即 example.com/index.html。

因此,为了解决这个问题,您需要直接设置 window.location.href,就像设置任何其他变量一样,例如:window.location.href = "http://example.com/redirect.html";

将一个变量分配给它将以完全相同的方式工作,如下所示:

var url = "http://example.com/";
// manipulate the url variable
window.location.href = url;

接下来,window.location.href将被设置为url(如果您没有对变量进行任何操作,则该值将为http://example.com/


你如何直接分配它? - PeanutsMonkey
就像你处理其他变量一样处理它。例如:window.location.href == "http://example.com/redirect.html";。我已经编辑了答案以包含这个内容。 - strugee
你如何动态地分配值呢? - PeanutsMonkey
我不确定你的意思。window.location.href只是一个字符串,所以你可以将它赋值给任何字符串变量。例如:`var url = "http://example.com/"; window.location.href = url;`将会把 http://example.com/ 赋值给 window.location.href - strugee

0
您可以使用以下解决方案来解决您的问题:
window.location.replace('@Url.Action("Action", "Controller")')

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