2017-08-27 92 views
-2

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

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

我会很感激,如果有人能告诉我该怎么做,以及如何将其正确写下一个整体。谢谢!

+0

'了window.location = window.location.slice(0,-5)+“alt.html'' – adeneo

+0

document.URL ??? – epascarello

回答

0

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" 
})