Javascript 为什么在ie中添加window.location而不是替换URL
我在ie中得到了错误的URL,但在firefox和chrome中没有 基本上,我有一个名为文本搜索的文本字段。我在htaccess中使用jQuery和rewriterule对页面进行内部重定向。我在localhost上,所有文件都在一个名为test的文件夹中 在firefox和chrome中,如果在文本搜索框中输入“hello”并点击enter,“hi”并点击enter和“再见”,则会得到正确的URL,如下所示: localhost/test/testing/hello 及 localhost/test/testing/hi 及 本地主机/测试/测试/再见 分别地 你得到了什么 localhost/test/testing/hello 及 localhost/test/testing/testing/hi 及 本地主机/测试/测试/测试/测试/再见 分别 这里的问题是“测试”是预先准备好的。如何阻止ie出现这种情况。我在网上找不到这个问题的答案 html和jquery代码Javascript 为什么在ie中添加window.location而不是替换URL,javascript,jquery,.htaccess,Javascript,Jquery,.htaccess,我在ie中得到了错误的URL,但在firefox和chrome中没有 基本上,我有一个名为文本搜索的文本字段。我在htaccess中使用jQuery和rewriterule对页面进行内部重定向。我在localhost上,所有文件都在一个名为test的文件夹中 在firefox和chrome中,如果在文本搜索框中输入“hello”并点击enter,“hi”并点击enter和“再见”,则会得到正确的URL,如下所示: localhost/test/testing/hello 及 localhost/
<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>
你能帮我一下吗。非常感谢
窗口。位置
不是字符串,我会非常小心地使用它-它实际上是一个位置
对象
也许这会帮助你:
var href = window.location.href;
window.location = href.replace(/testing\/.*$/, "testing/"+$('#text-search').val());
您还可以执行以下操作:
var href = "" + window.location;
强制转换字符串,并按值传递。如果您这样设置:
window.location.href='/yourpage'
它将附加url。要避免这种情况,请使用/
而不是/
所以它看起来像:
window.location.href='//yourpage'
考虑url栏中的当前url
<>我们将考虑两个案例
第一个**window.location='伪字符串'**
第二个**window.location='/另一个伪字符串'**
然后在第一种情况下,它将被附加到URL中,而在第二种情况下,它将替换URL中的路径名(即/marketing)。您好,谢谢您的帮助。我不得不将regex改为/testing.*$/因为最初我有localhost/test/testing.php和afterword,我得到了localhost/test/testing/…,很抱歉上面没有说清楚。很好用。再次感谢。
var href = "" + window.location;