2011-10-19 4 views
0

나는이 코드를 사용하고 있습니다 :Ajaxpageloak는 수있을 때 (예 : # 1)

<script type='text/javascript'> 
$(document).ready(function() { 

    //Check if url hash value exists (for bookmark) 
    $.history.init(pageload); 

    //highlight the selected link 
    $('a[href=' + document.location.hash + ']').addClass('selected'); 

    //Seearch for link with REL set to ajax 
    $('a[rel=ajax]').click(function() { 

     //grab the full url 
     var hash = this.href; 

     //remove the # value 
     hash = hash.replace(/^.*#/, ''); 

     //for back button 
     $.history.load(hash); 

     //clear the selected class and add the class class to the selected link 
     $('a[rel=ajax]').removeClass('selected'); 
     $(this).addClass('selected'); 

     //hide the content and show the progress bar 
     $('#ajax').hide(); 
     $('#loading').show(); 

     //run the ajax 
     getPage(); 

     //cancel the anchor tag behaviour 
     return false; 
    }); 
}); 


function pageload(hash) { 
    //if hash value exists, run the ajax 
    if (hash) getPage(); 
} 

function getPage() { 

    //generate the parameter for the php script 
    var data = 'page=' + document.location.hash.replace(/^.*#/, ''); 
    $.ajax({ 
     url: "loader.php", 
     type: "GET",  
     data: data,  
     cache: false, 
     success: function (html) { 

      //hide the progress bar 
      $('#loading').hide(); 

      //add the content retrieved from ajax and put it in the #content div 
      $('#ajax').html(html); 

      //display the body with fadeIn transition 
      $('#ajax').fadeIn('slow');  
     }  
    }); 
} 
</script> 

그래서 내가 사용해야 ... 페이지를 아약스를 실행을하지만 어떤 장소에서 사용하고 있습니다 :

예를 들어, <a href='#1'> notices </ a>으로 이동하십시오. 그리고 id = '1'로 운전하는 대신, 클릭하여 실행하는 아약스 코드를 수행하고 있습니다.

해시에서 코드 번호를 실행할 때 예외를 어떻게 추가합니까?

답변

1

, 당신은 쉽게 정규식을 사용할 수 있습니다. 당신이 문자열을 패턴을 작성하고 확인할 수 있습니다 이 방법 :

var pattern = new RegExp(/.*[0-9].*/); 
pattern.test(hash); // Will return TRUE if it contains any digit. 
+0

이 작품 .. :) 나는 추가 : \t var 패턴 = 새 RegExp (/.* [0-9]. * /); if (pattern.test (hash)! = true) {} –

1

해시가 정수인지 확인하기 위해 검사를 추가하여 예외를 추가 할 수 있습니다. 아래 코드를 참조하십시오. (난 단지처럼 코드의 나머지 부분은 괜찮의 $('a[rel=ajax]') 부분을 조정했습니다.) 당신이 당신의 해쉬 문자열에서 숫자의 존재를 확인하려는 경우

//Search for link with REL set to ajax 
$('a[rel=ajax]').click(function() { 

    //grab the full url 
    var hash = this.href; 

    //remove the # value 
    hash = hash.replace(/^.*#/, ''); 

    // test if hash is not an integer 
    if(parseInt(hash) != hash){ 

     //for back button 
     $.history.load(hash); 

     //clear the selected class and add the class class to the selected link 
     $('a[rel=ajax]').removeClass('selected'); 
     $(this).addClass('selected'); 

     //hide the content and show the progress bar 
     $('#ajax').hide(); 
     $('#loading').show(); 

     //run the ajax 
     getPage(); 

     //cancel the anchor tag behaviour 
     return false; 
    } 
}); 
+0

그래, 내가 필요한거야 ... 해시는 하나의 수를 포함 여부 확인, 나는 당신의 예제를 사용하고 작동하지 않았다 ... 나는 JavaScript로 평신도이다. –

+0

예제가 작동하지 않아 죄송합니다. 원래 코드로 게시 한 코드의 한 섹션 만 대체 했습니까? – whoisgregg

+0

예, 코드가 이미 실행 중입니다. 그러나 "if (parseInt (hash)! = hash)"가 작동하지 않습니다 :/ –

관련 문제