2017-02-23 21 views
0

我試圖讓重定向,並通過#參數JavaScript重定向與.hash

// I'm at: http://example.com/index.html 
var url = 'http://example.com/category.html'; 
var hash = '#testsite'; 

// Ignores # and all after 
window.location.href = url + hash; 

// Does not work, sets hash first, then redirects to url without maintaining hash 
window.location.href = url; 
window.location.hash = hash; 

是否可以像使用javasript重定向window.location.url?

+0

你檢查我的答案?如果它是正確的,請接受它。謝謝! – Dekel

回答

0

也許你可以嘗試像下面

var url = 'http://example.com/category.html'; 
var hash = '#testsite'; 
window.open(url+"?hash="+hash,"_self"); 
//http://example.com/category.html?hash=#testsite 
+0

如果我沒有錯,它會通過#testsite作爲查詢參數。對?我不想不幸.. Thx爲你的努力壽! – Grzegorz

1

不知道什麼是templocation但是這個偉大的工程:

var url = 'http://example.com/category.html'; 
 
var hash = '#testsite'; 
 

 
window.location.href = url + hash;

+0

剛注意到,它是加載後立即刪除我的#和後的網站。必須研究它是如何去除它的......我在最後一刻把我的頭髮撕掉,爲什麼它不起作用。 – Grzegorz

+0

不知道我明白你想說什麼...... – Dekel

+0

我的意思是,重定向是正確的......但是另一個腳本在頁面加載後立即從url中刪除它。所以我沒有看到它,並認爲它沒有被通過。 – Grzegorz