javascript 为什么 window.location 附加而不是替换 ie 中的 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19186000/
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
why window.location appending instead of replacing the URL in ie
提问by Hani
I am getting wrong URLs in ie but not in firefox and chrome.
我在 ie 中收到错误的 URL,但在 firefox 和 chrome 中没有。
Basically, I have a textfield called text-search. I am using jQuery and rewriterule in htaccess to internally redirect pages. I am on localhost and all files are in a folder called test.
基本上,我有一个名为 text-search 的文本字段。我在 htaccess 中使用 jQuery 和 rewriterule 来内部重定向页面。我在本地主机上,所有文件都在一个名为 test 的文件夹中。
In firefox and chrome, if you enter 'hello' hit enter, 'hi' hit enter and 'goodbye' hit enter in the text-search box you get the correct URLs as
在 firefox 和 chrome 中,如果您在文本搜索框中输入“hello”按 Enter、“hi”按 Enter 和“goodbye”按 Enter 则会得到正确的 URL
localhost/test/testing/hello
本地主机/测试/测试/你好
and
和
localhost/test/testing/hi
本地主机/测试/测试/嗨
and
和
localhost/test/testing/goodbye
本地主机/测试/测试/再见
repectively.
分别。
In ie you get
在即你得到
localhost/test/testing/hello
本地主机/测试/测试/你好
and
和
localhost/test/testing/testing/hi
本地主机/测试/测试/测试/嗨
and
和
localhost/test/testing/testing/testing/goodbye
本地主机/测试/测试/测试/测试/再见
respectively
分别
The problem here is that 'testing' is prepending. How to stop this from happening in ie. I could not find an answer to this problem on the web.
这里的问题是“测试”是预先准备好的。如何阻止这种情况发生在 ie 中。我在网上找不到这个问题的答案。
html and jquery code
html 和 jquery 代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<base href="http://localhost/test/" />
<script src="jQuery.js"></script>
<script>
$(document).ready(function(){
$("#text-search").keyup(function(e){
if (e.keyCode == 13){
window.location.replace("testing/"+$('#text-search').val());
}
})
})
</script>
</head>
<body>
<input type='text' id='text-search'>
</body>
</html>
.htaccess
.htaccess
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^testing/(.+)$ /test/testing.php?string= [L]
Can you please help me on this. Many thanks
你能帮我解决这个问题吗?非常感谢
回答by Halcyon
window.location
isn't a string, I'd be very careful with using it that way - it's actually a Location
object.
window.location
不是字符串,我会非常小心地使用它 - 它实际上是一个Location
对象。
Maybe this will help you:
也许这会帮助你:
var href = window.location.href;
window.location = href.replace(/testing\/.*$/, "testing/"+$('#text-search').val());
You can also do:
你也可以这样做:
var href = "" + window.location;
to force the string cast, and pass-by-value.
强制字符串强制转换,并按值传递。
回答by Charitha Goonewardena
If you set like this: window.location.href = '/yourpage'
如果你这样设置: window.location.href = '/yourpage'
it will append the url. To avoid that use //
instead of /
它将附加网址。为了避免这种使用//
而不是/
so it will look like :window.location.href = '//yourpage'
所以它看起来像:window.location.href = '//yourpage'
回答by Sandeep Kumar
considering currenturl in url bar be www.theunusualcards.com/marketing.
考虑到 url 栏中的 currenturl 是www.theunusualcards.com/marketing。
we will consider two cases
我们将考虑两种情况
first **window.location = 'dummy-string'**
第一的 **window.location = 'dummy-string'**
second **window.location = '/another-dummy-string'**
第二 **window.location = '/another-dummy-string'**
then in the first case, it will get appendedto URL further while in the second case it will be replacingpathname (i.e /marketing) in URL.
然后在第一种情况下,它将进一步附加到 URL,而在第二种情况下,它将替换URL 中的路径名(即/marketing)。