2012-08-10 11 views
2

Google 폐쇄 코드에 문제가 있습니다. HTML5 기록 기능을 사용하여 앱을 구현하려고합니다.정의되지 않은 'HTML5HISTORY'속성을 읽을 수 없습니다.

하지만 불행히도 Chrome에서 '속성을 읽을 수 없습니다'HTML5HISTORY '가 표시되고 Firefox에서'goog.history is undefined '가 표시되어 작동하지 않습니다.

기본적으로 "var h"는 "try/catch"루프에서 오류가 발생하면서 History 인스턴스로 생성되지 않으며 error.message가 표시됩니다.

정말 문제가 무엇인지 알 수

...

덕분에

goog.require('goog.events'); 
goog.require('goog.events.EventTarget'); 
goog.require('goog.history.EventType'); 
goog.require('goog.history.Html5History'); 
goog.require('goog.Uri'); 

var h; 

try { 

h = new goog.history.Html5History(); 

} catch (e) { 

    document.write(e.message); 
} 

if (h) { 

    var cur = 'kittens'; 

    goog.events.listen(h, goog.history.EventType.NAVIGATE, function(e) { 
     var token = e.token || 'kittens'; 

     var next = document.getElementById(token); 

     if (next) { 

      document.getElementById(cur).className = 'section'; 
      next.className = 'section active'; 
      cur = token; 
     } 
    }); 

    h.setUseFragment(false); 
    h.setPathPrefix(new goog.Uri(document.location.href).getPath() + '/'); 
    h.setEnabled(true); 

    goog.events.listen(document.getElementById('links'), 'click', function(e) { 

     if (e.target.tagName == 'A') { 

      h.setToken(e.target.getAttribute('token'), e.target.title); 
      e.preventDefault(); 
     } 
    }); 
} 

답변

2

이 전에 별도의 자바 스크립트 파일 또는 태그의 모든 수입 (goog.require()를 호출)를 포함 얘들 아 당신 스크립트 포함.

Documentation는 말한다 :

Note: Do not put your goog.require() statements in the same script tag as the entry point to code that uses the goog.required functions or classes. A goog.require() call adds code to the document after the script tag containing the call. For example, this code works:

<script src="closure-library/closure/goog/base.js"></script> 
<script> 
    goog.require('goog.dom'); 
</script> 
<script> 
    var newHeader = goog.dom.createDom('h1'); 
</script> 

In contrast, the following code produces an error:

<script src="closure-library/closure/goog/base.js"></script> 
<script> 
    // DON'T DO THIS. 
    goog.require('goog.dom'); 
    var newHeader = goog.dom.createDom('h1'); 
</script> 
+0

신난다! 고마워 :) – socrateisabot

+2

번갈아, 번들 또는 코드를 컴파일하십시오. – John

+0

대개 내 프로젝트에서 deps.js 파일을 으로 생성했습니다. DepsWriter https://developers.google.com/closure/library/docs/depswriter base.js 다음에 내 deps.js를 포함하고 있습니다. 나머지 파일. 컴파일하기 전에는 모두 디버그 모드입니다. 제작을 위해 나는 deps.js없이 하나의 컴파일 된 파일 만 사용하고 있습니다. –

관련 문제