2011-09-29 8 views

답변

8
var h = location.hash.substr(1); 
if (h == 'functionplease') 
    functionplease(); 

substr (1) 해시로 #을 제거하십시오.

많은 기능이 있습니까? 문제 없어!

switch(location.hash.substr(1)) { 
    case 'buy': buy(); break; 
    case 'sell': sell(); break; 
    case 'other': other(); break; 
} 
+0

완벽한, 감사합니다! 폴링 대신 – Cordial

0

다음 작업을해야합니다 :

<script type="text/javascript"> 
    //check if hash tag exists in the URL 
    if(window.location.hash) 
    { 
     //set the value as a variable, and remove the # 
     var hash_value = window.location.hash.replace('#', ''); 

     //check value of hash 
     if (hash_value == 'functionplease'){ 
      // execute code 
     } 
    } 
</script> 

희망이 도움이!

0
var pathname = window.location.pathname; 
if(pathname.indexOf("#") != -1){ 
    //call your function 
} 
0

시도 :

<script type="text/javascript"> 
    window.onload = function() { 
     setInterval(pollHash, 100); 
    } 


    var lasthash = ''; 
    function pollHash() { 
     if (window.location.hash == lasthash) { return; } 

     lasthash = window.location.hash; 
     console.log('hash is changed! Current hash is: ' + lasthash); 

     switch (lasthash) { 
      case '???' : 
       /* do something */ 
       break; 
      default: 
       /* do nothing */ 
       break; 
     } 
    } 

</script> 

가 좀 있습니다 http://ajaxpatterns.org/Unique_URLs#Unique_URL_Sum_Demo

+0

이 onhashchange 이벤트를 사용합니다. https://developer.mozilla.org/en/docs/Web/API/WindowEventHandlers/onhashchange – rtack

관련 문제