URL에 특정 해시가있는 페이지가로드 될 때 함수를 실행하려고합니다.jQuery : URL에 특정 해시가있는 경우 함수를 실행 하시겠습니까?
그래서 www.mypage.com/hello.html#functionplease
을 방문하면 코드가 실행됩니다.
누구나 올바른 방향으로 나를 가리킬 수 있습니까?
감사합니다.
URL에 특정 해시가있는 페이지가로드 될 때 함수를 실행하려고합니다.jQuery : URL에 특정 해시가있는 경우 함수를 실행 하시겠습니까?
그래서 www.mypage.com/hello.html#functionplease
을 방문하면 코드가 실행됩니다.
누구나 올바른 방향으로 나를 가리킬 수 있습니까?
감사합니다.
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;
}
다음 작업을해야합니다 :
<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>
희망이 도움이!
var pathname = window.location.pathname;
if(pathname.indexOf("#") != -1){
//call your function
}
시도 :
<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
이 onhashchange 이벤트를 사용합니다. https://developer.mozilla.org/en/docs/Web/API/WindowEventHandlers/onhashchange – rtack
완벽한, 감사합니다! 폴링 대신 – Cordial