window.location.href不会更改URL

时间:2012-11-18 19:54:21

标签: javascript

我想更改表单提交后出现的网址,尽管下面的代码似乎没有按照我的意愿去做。如何更改地址栏中的URL?

CODE

<!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>

4 个答案:

答案 0 :(得分:3)

要更改网址,您必须执行以下操作:

window.location.href = testurl;

但这会产生使用新网址加载整个网页的“副作用”(无论是否相同)。

如果您想更改显示但未重新加载页面的内容,则可能需要使用pushState来使用新的HTML5历史记录API。这就是在许多Ajax站点中所做的,让URL反映动态加载页面中的内容,但它与IE9不兼容 - 。

var stateObj = { foo: "bar" };
history.pushState(stateObj, "some useless title", testurl);

答案 1 :(得分:2)

尝试以下方法:

<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相同的值

答案 2 :(得分:1)

tl; dr:testurl不是对window.location.href的引用(它们具有相同的值),因此您需要将您的网址直接指定给window.location.href而不是{{ 1}}。例如:testurl

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

您的问题正在发生,因为您将位置分配到window.location.href = "http://example.com/redirect.html";,而不是testurl。让我解释一下:

  1. window.location.hrefvar testurl = window.location.href;(即example.com/index.html)的值分配给变量window.location.href。因此,此时testurltesturl设置为example.com/index.html,但它们彼此无关(window.location.href不是testurl的引用1}},它只有相同的值)
  2. window.location.hreftesturl == testurl.replace(...);替换为您要重定向到的网站,但由于testurl 是对testurl的引用,{{ 1}}成为example.com/redirect.html,window.location.href保持原有状态,即example.com/index.html。
  3. 因此,为了解决此问题,您需要直接设置testurl,就像您将其他任何变量一样,例如window.location.href

    为其分配变量将以完全相同的方式工作,如下所示:

    window.location.href

    然后,window.location.href = "http://example.com/redirect.html";将被设置为var url = "http://example.com/"; // manipulate the url variable window.location.href = url; 的任何内容(如果您根本没有操作变量,那么http://example.com/将会是window.location.href

答案 3 :(得分:0)

您可以使用以下解决方案来解决您的问题:

window.location.replace('@Url.Action("Action", "Controller")')
相关问题