window.location.href = window.location.href重新整理頁面的問題

來源:互聯網
上載者:User
論window.location.href = window.location.href重新整理頁面的問題

在JS中可以用window.location.href = window.location.href來重新整理頁面(當然還有很多其他的方法),

在使用這個方法重新整理頁面的時候回出現問題,如下:

<body onload='alert("aaa");'>   <script>function refresh()   {             window.location.href = window.location.href;   }   </script>     <a href ='#'>###</a>   <input type ='button' value='button' onclick='refresh();' />   </body>   

分析這段代碼你會發現,當點擊###後,再點擊Button頁面不會重新整理。

想必看到這裡你已經知道問題所在,就是在點擊###後,Url上會加上一個#,

於是refresh()方法就變成了 window.location.href ="www.something.com/test.aspx#"

成為了一個錨點。

我的解決方案也很簡單:

window.location.href = window.location.href.replace(/#/g,'');   

替Url中的換所有#

一般來說這樣替換不會有問題,url中一般不含有#,替換後也不會導致連結失效。

PS:雖然只是一個小問題,但開發時還是要注意。

每個人都會經過這個階段,見到一座山,就想知道山後面是什麼。我很想告訴他,可能翻過山後面,你會發現沒什麼特別。回望之下,可能會覺得這一邊更好。 每個人都會堅持自己的信念,在別人看來,是浪費時間,她卻覺得很重要。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.