2017-02-23 48 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