javascript window.location.href 不会改变 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13443974/
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-10-26 18:48:06  来源:igfitidea点击:

window.location.href does not change the URL

javascript

提问by PeanutsMonkey

I would like to change the URL that appears once a form has been submitted although the code below does not appear to do what I would like it to do. How do I change the URL in the address bar?

我想更改提交表单后出现的 URL,尽管下面的代码似乎没有做我想做的事情。如何更改地址栏中的 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>

采纳答案by KingKongFrog

Try the following:

请尝试以下操作:

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

A note:

一张纸条:

Use window.location for read and write access to the location object associated with the current frame. If you just want to get the address as a read-only string, you may use document.URL, which should contain the same value as window.location.href

使用 window.location 对与当前帧关联的位置对象进行读写访问。如果您只想将地址作为只读字符串获取,您可以使用 document.URL,它应该包含与 window.location.href 相同的值

回答by Denys Séguret

To change the url, you have to do this :

要更改网址,您必须这样做:

window.location.href = testurl;

but this has the "side effect" of loading the whole page with the new url (be it or not the same).

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

If you want to change what appears but not reload the page, you hage to use pushStateto use the new HTML5 history API. That's what's done in many Ajax sites to let the URL reflect what's in the dynamically loaded page but it's not compatible with IE9-.

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

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

回答by strugee

tl;dr: testurlisn't a reference to window.location.href(they just have the same value), so you need to assign your URL directly to window.location.hrefinstead of testurl. For example: window.location.href = "http://example.com/redirect.html";

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

Basically, let's say that your web site was at example.com/index.html, and you wanted to redirect to example.com/redirect.html.

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

Your problem is occurring because you're assigning the location to testurl, not window.location.href. Let me explain:

出现问题是因为您将位置分配给testurl,而不是window.location.href。让我解释:

  1. var testurl = window.location.href;assigns the value of window.location.href(which is example.com/index.html) to the variable testurl. So at this point, testurland window.location.hrefare set to example.com/index.html, but they have nothing to do with each other (testurlisn't a reference to window.location.href, it just has the same value)
  2. testurl == testurl.replace(...);replaces testurlwith the site you want to redirect to, but since testurlis nota reference to window.location.href, testurlbecomes example.com/redirect.html and window.location.hrefstays what it was previously, which was example.com/index.html.
  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.hreftesturlexample.com/redirect.html变得和window.location.href住宿它是什么之前,这是example.com/index.html。

So in order to fix this, you need to set window.location.hrefdirectly, just like you would any other variable, e.g. window.location.href = "http://example.com/redirect.html";.

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

Assigning a variable to it will work in the exact same way, like so:

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

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

Then, window.location.hrefwill be set to whatever urlis (which will be http://example.com/if you didn't manipulate the variable at all)

然后,window.location.href将设置为任何url值(如果您根本没有操作变量,它将是http://example.com/

回答by Yojana Ambavkar

You can use the following solution to solve your problem:

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

window.location.replace('@Url.Action("Action", "Controller")')