我有一個站點example.com。 當我訪問此頁面example.com/#login我想調用一個js警報函數。 我該怎麼做?當url包含錨點時如何調用js函數
0
A
回答
0
您可以檢查#login的URL。如果存在,則調用該函數。 http://www.webmasterworld.com/forum91/216.htm
我建議jQuery的,而這種做法: http://jquery-howto.blogspot.co.uk/2009/09/get-url-parameters-values-with-jquery.html
5
// example.com/#login
if (window.location.hash) {
alert("URL has hash");
var hash = window.location.hash; // = "#login"
callMyFunction();
}
1
如果你想要處理的URL是一個很好的跨瀏覽器庫,我會考慮History.js
https://github.com/browserstate/History.js/
History.Adapter.bind(window,'statechange',function(){
// Note: We are using statechange instead of popstate
var State = History.getState();
// Note: We are using History.getState() instead of event.state
History.log(State.data, State.title, State.url);
});
而且還有Backbone.js,其中ma因爲它是一個MV *框架,但它具有路由器的概念,這也是有用的。
0
我想你可以檢查是否window.location.hash
不是falsy值來檢查是否存在URL中的哈希值。
例如:
function someAlert(message) {
// This would be your alert function. This is just for example
alert(message);
}
// The "if the URL has a hash":
if (window.location.hash) {
someAlert("Hello URL with a hash visitor!"); // "Hello URL with a hash visitor!" can be whatever you want
}
如果你想保存無論是後包括散列,只需使用:
var hash = window.location.hash; // hash would be "#login" if the URL was http://example.com/#login
相關問題
- 1. 僅當錨點的URL包含
- 2. 當標籤包含重複'ID'屬性時調用JS函數
- 3. 如何調用該函數節點JS
- 4. 當錨包含圖像
- 5. 當url包含#
- 6. Js:點擊調用函數
- 7. 節點js調用函數
- 8. 當()包含在調用中時,JavaScript遞歸函數中斷
- 9. 如何在需要時調用函數,每個函數包含PHPUnit函數
- 10. Javascript通過錨點調用函數
- 11. 如何調用JS函數
- 12. 如何在php中調用一個函數時包含文件
- 13. jquery:使用:包含錨點標記
- 14. 如何調用包含完成處理函數的函數?
- 15. 如何從客戶端調用JavaScript函數錨點擊
- 16. 當url包含非英語語言時如何使用pycurl?
- 17. 當初始選擇器包含子時,Jquery slideUp回調函數
- 18. 調用.js文件中包含的JavaScript函數
- 19. 當調用對象時沒有方法的JS調用函數
- 20. 如何爲特定錨點設置偏移錨點函數?
- 21. 節點Js回調函數
- 22. 當我點擊「提交」按鈕時,如何調用Javascript函數?
- 23. 同時調用函數和onpage錨點<button>
- 24. 如何使用url運行js函數?
- 25. 如何在調用函數調用次數時調用函數中的閉包
- 26. 當構建URL時,URL.Action包含id
- 27. JS如何確定何時調用回調函數?
- 28. 3個請求函數的數組 - 僅當第一個函數的響應包含字符串時如何調用第二個函數? - Node JS
- 29. 如何包含函數
- 30. 如何在main中調用函數包含對節點的引用