2012-04-30 8 views

答えて

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もありますそれはMV *フレームワークなので、あなたが必要とする以上のものになるでしょう。しかし、それはまた役に立つルータの概念を持っています。

http://backbonejs.org/#Router

0

私はwindow.location.hashは、URL内のハッシュがあるかどうかを確認するためにfalsy値でないかどうかをチェックすることができると思います。例えば

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 
関連する問題