2011-02-11 11 views
11

pushState를 사용할 수 있는지 여부를 결정하는 라이브러리를 아는 사람이 있습니까?pushState를 사용할 수 있습니까?

것은 나는 이것을 사용했다 : http://support.github.com/discussions/site/2263-line-links-broken가 :

if(window.history.pushState){ 
    window.history.pushState(null, document.title, path); 
}else{ 
    location.pathname = path; 
} 

하지만 난 그냥 위의 테스트를 통과했지만조차 작동하지 않을 것이 원인이 사파리 5.0.2에 버그가 있다는 사실을 발견했다.

다른 문제가있을 수 있으며 누군가 이미 문제를 발견하고 해결했지만 아직 어떤 것도 발견하지 못했다고 생각합니다.

편집 : pushState는 역사 스택에 밀어 URL을 변경하지만 location.pathname를 업데이트하지 않습니다 것 같다 무엇을 본 적이에서 @Crescent 신선한

. 내 코드에서 setInterval을 사용하여 경로가 업데이트되었는지 확인합니다.

var cachedPathname = location.pathname; 
if(window.history.pushState){ 
    cachedPathname = location.pathname; 
    setInterval(function(){ 
     if(cachedPathname !== location.pathname){ 
      cachedPathname = location.pathname; 
      //do stuff 
     } 
    }, 100); 
} 

Safari 5.0.2에서 pushState가 url을 변경하면 location.pathname이 변경되지 않습니다. 이 기능은 Safari의 다른 브라우저 및 버전에서 작동합니다.

+0

'window.history.pushState'에 대한 테스트가 잘못된 것으로 링크 된 페이지의 어떤 부분이 잘못 되었습니까? 버그는 특정 github이하는 일과 관련이있는 것 같습니다 ('location.hash'를 설정하여 pushState를 수집 할 수 있습니다). –

답변

16

pushState는 HTML5 History API의 일부입니다. 당신과 같이 일반 자바 스크립트를 사용하여 지원을 테스트 할 수 있습니다

if (typeof history.pushState !== "undefined") { 
    // pushState is supported! 
} 

을 다른 방법으로, Modernizr 라이브러리를 사용할 수 있습니다 : 이것은 푸시 상태를 확인하는 방법입니다 모더 나이저 소스 코드를 보면

if (Modernizr.history) { 
    // pushState is supported! 
} 
24

을 :

tests['history'] = function() { 
     return !!(window.history && history.pushState); 
    }; 

그래서 당신을위한 간단한 방법은 다음과 같습니다

두 단계로 진행하기 전에 먼저 window.history의 존재를 확인해야하며 이는 아마도 오류가 발생한 이유 일 것입니다.

+1

실제로 (!! window.history && !! history.pushState) .... (window.history && history.pushState) 자체가 false/true를 반환합니다 ... 사용하지 않는 점 !! outside ... .. –

+0

대안은'window.history && 'pushState'in window.history'입니다. – sn3p

관련 문제