Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更改当前URL,然后加载_Javascript_Jquery_String_Url_Slice - Fatal编程技术网

Javascript 更改当前URL,然后加载

Javascript 更改当前URL,然后加载,javascript,jquery,string,url,slice,Javascript,Jquery,String,Url,Slice,我希望jquery找到并更改您正在访问的当前URL,然后加载新的URL。假设jquery应该在单击后将当前的“index.html”更改为“indexalt.html”,然后加载它。我的想法是使用document.URL获取当前URL,然后在末尾切掉“.html”,并在字符串中添加例如“alt.html”。我对jquery非常陌生,无法让它工作。我的脚本中可能会有很多错误: $("#contact").on('click', function() { var url=document.URL(s

我希望jquery找到并更改您正在访问的当前URL,然后加载新的URL。假设jquery应该在单击后将当前的“index.html”更改为“indexalt.html”,然后加载它。我的想法是使用document.URL获取当前URL,然后在末尾切掉“.html”,并在字符串中添加例如“alt.html”。我对jquery非常陌生,无法让它工作。我的脚本中可能会有很多错误:

$("#contact").on('click', function() {
var url=document.URL(str.slice(-7));
.load("url +"alt.html"");

如果有人能告诉我怎么做,怎么把它完整地写下来,我将不胜感激。谢谢

location.href
返回页面的URL。所以,你可以这样做

$("#contact").on("click", function() {
    // Slicing 5 characters from the URL.
    const url = location.href.slice(0, -5)
    // Simply load the URL
    location.href = url + "alt.html"
})

window.location=window.location.slice(0,-5)+'alt.html'
document.URL???