2014-10-29 31 views
0

後,我有這個頁面在這裏:jQuery的從URL中移除哈希,將部分

http://raidersleafs.com/andreaparliamentlaw/professional-development.html

如果你點擊頂部的菜單項之一,可以說,「商業法」

會重定向到這裏:

http://raidersleafs.com/andreaparliamentlaw/index.html#business

該網站的正確部分,但我期待刪除#busness在URL中,同時仍然到該部分。我已經嘗試window.location.hash="",這工作,但它並沒有把我帶到網站的頂部,而不是商業法部分....我希望這是有道理的,這可能嗎?

感謝,

+0

是的......這是一個正常的行爲...只是防止違約行爲......?但推薦使用hask來鏈接該頁面。 – Loenix 2014-10-29 20:37:27

回答

2

你可以這樣做:

y = window.scrollY; 
window.location.hash=""; 
window.scrollTo(0, y); 

它獲取當前滾動位置,改變散,然後滾動回到它原來所在之處。

或者你可以做這樣的:

history.pushState('', document.title, window.location.pathname); 

將在URL的末尾擺脫#的爲好,然後你不必與滾動位置的混亂。

+0

嗨,我試過了你的兩個解決方案,第一個仍然在URL中有#並且不滾動,第二個完全刪除#,但是沒有轉到頁面加載後正在運行的部分 – user979331 2014-10-30 00:57:51

+0

,右? '$(window).on('load',function(){history.pushState('',document.title,window.location.pathname);});' – dave 2014-10-30 02:25:43

+0

yup that worked :)謝謝。 – user979331 2014-10-30 12:51:03