2017-08-27 95 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" 
})