2011-11-28 90 views
1

我使用jquery.history lugin在這裏找到:http://tkyk.github.com/jquery-history-plugin/jquery歷史插件不包括腳本?

有了這個我加載內容到一個div。但是,腳本(使用chrome webtools分析後)不包含腳本。假設我有一個名爲#content的div。如果我想page.html中加載到它和HTML頁面包含一個JavaScript代碼或包括(作爲示例)就像這樣:

<script type="text/javascript" src="jquery.js"></script>  
<script type="text/javascript"> 
    alert("hello world"); 
</script> 

它不會加載的jquery.js或執行警報。在webtools我反而看到它是這樣的:

你明白了。沒有。 所以它似乎排除JavaScript以某種方式,只挑出了HTML。在我jqeury.history的代碼,我只加載page.html中的某一格(.TARGET),它看起來像這樣:

(function($){ 
     var origContent = ""; 

     function loadContent(hash) { 

      $('#content').stop(true,true).fadeOut(); 
      if(hash != "") { 

       if(origContent == "") { 
        origContent = $('#content').html(); 
       } 
       $('#content').load(hash +".html .target", 
            function(){ $(this).prettyPrint(); }); 
      } else if(origContent != "") { 
       $('#content').html(origContent); 
      } 
      $('#content').stop(true,true).fadeIn("fast"); 
     } 

     $(document).ready(function() { 
       $.history.init(loadContent); 

       $('#navigation a').not('.external-link').click(function(e) { 

         var url = $(this).data('name'); 
         url = url.replace(/^.*#/, ''); 
         $.history.load(url); 
         return false; 

        }); 
      }); 

    })(jQuery); 

最好的問候!

回答

1

jQuery「.load()」方法去掉<script>元素,並且在URL包含選擇器時不會運行它們。

  $('#content').load(hash +".html .target", 
           function(){ $(this).prettyPrint(); }); 

看看這個URL如何在實際URL(和分隔空間)之後有「.target」選擇器?這就是觸發行爲的原因。

我前一段時間記錄了一個關於這個的bug,結論是更新文檔。我認爲腳本被拋棄的原因是,當「.load()」只是取出一部分加載內容時,不能確定腳本不會依賴於所選子集之外的其他腳本。

+0

但是沒有辦法解決這個問題嗎?一種加載腳本標記的方法,即使它自然不允許它? – Paparappa

+0

你基本上必須自己嘗試並實現它。沒有標誌或選項或任何可讓您告訴「.load()」運行腳本的內容。 – Pointy

+1

[這是jQuery的bug。](http://bugs.jquery.com/ticket/6307) – Pointy