为什么 window.location 追加而不是替换 IE 中的 URL

why window.location appending instead of replacing the URL in ie

本文关键字:替换 IE 中的 URL window location 追加 为什么      更新时间:2023-09-26

我在IE中得到错误的URL,但在Firefox和chrome中没有。

基本上,我有一个名为文本搜索的文本字段。我正在使用jQuery和rewriterule在htaccess内部重定向页面。我在本地主机上,所有文件都在一个名为test的文件夹中。

在Firefox和chrome中,如果您在文本搜索框中输入"hello"按回车键,"hi"按回车键和"再见"按回车键,您将获得正确的URL,如下所示

本地主机/测试/测试/你好

本地主机/测试/

测试/嗨

本地主机/测试/

测试/再见

重复。

在即你得到

本地主机/测试/测试/你好

本地主机/测试/

测试/测试/嗨

本地主机/测试/测试

/测试/测试/再见

分别

这里的问题是"测试"是预先置的。如何阻止这种情况在IE中发生。我在网上找不到这个问题的答案。

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

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^testing/(.+)$ /test/testing.php?string=$1 [L]

你能帮我这个吗?非常感谢

如果这样设置:window.location.href = '/yourpage'

它将附加网址。为避免这种情况,请使用//而不是/

所以它看起来像:window.location.href = '//yourpage'

window.location不是

字符串,我会非常小心地以这种方式使用它 - 它实际上是一个Location对象。

也许这会帮助你:

var href = window.location.href;
window.location = href.replace(/testing'/.*$/, "testing/"+$('#text-search').val());

您还可以执行以下操作:

var href = "" + window.location;
以强制字符串

强制转换和按值传递。

考虑到网址栏中的当前网址是 www.theunusualcards.com/marketing

我们将考虑两种情况

第一**window.location = 'dummy-string'**

第二**window.location = '/another-dummy-string'**

然后在第一种情况下,它将进一步附加到 URL,而在第二种情况下,它将替换 URL 中的路径名(即 /marketing(。