if(document.URL!="location.php?img_url="+img_url){
window.location.href = "location.php?img_url="+img_url;
}
這會不斷重新加載頁面。我檢查網址,看看它是否發生變化,但沒有。比較文檔時出現意外結果。網址
if(document.URL!="location.php?img_url="+img_url){
window.location.href = "location.php?img_url="+img_url;
}
這會不斷重新加載頁面。我檢查網址,看看它是否發生變化,但沒有。比較文檔時出現意外結果。網址
!=
總是正常工作。這是你誰工作錯了!
在這種情況下,問題是您期望document.URL
是您設置爲window.location.href
的字符串。它可能不是。
事實上,它永遠不會是"location.php?img_url="+img_url
。它將始終是完整的網址,所以類似http://www.example.com/location.php?img_url="+img_url
。
總是!正確的作品 – raam86
因爲看什麼document.URL
是!
console.log(document.URL);
它返回完整的url。 http://example.com/location.php?img_url=1234
您正在尋找完全匹配,而不是部分匹配。
一種解決方案是使用indexOf()
if(document.URL.toLowerCase().indexOf("location.php?img_url="+img_url)===-1){
嘗試使用
window.location.pathname
,而不是使用
document.URL
這不包括查詢字符串。 –
嘗試
if(window.location != "http://www.urlhere.com/location.php?img_url="+img_url){
window.location.href = "http://www.urlhere.com/location.php?img_url="+img_url;
}
剛編輯答案。我的意思是還包括完整的URL路徑。我只知道'window.location'比'document.URL'更容易 – copilot0910
也許你只是忘了域名。
嘗試這樣的事:
if(document.URL!="http://localhost/location.php?img_url="+img_url){
window.location.href = "http://localhost/location.php?img_url="+img_url;
}
使用document.location.pathname + document.location.search
代替document.URL
我向你保證的JavaScript的'='操作是否正常工作! – naomik
刪除了荒謬問題標題和無關標籤。 –