2011-12-01 89 views
1

我想重新啓動我在頭部鏈接的樣式和processing.js腳本,以便它們在通過ajax請求引入時正確顯示。我看到這個代碼需要在ajax請求中的哪個位置,但我不知道如何告訴代碼來簡單地重新應用腳本。我見過使用getScript()來做這件事的人,但是從我可以告訴的重新加載腳本,而不是簡單地告訴它重複或反思。所有的腳本都需要自己重新初始化嗎?我發現語法highlighters .highlight()方法,但我還沒有得到處理腳本加載。目前,Processing.loadSketchFromSources($('#processing'), ['mysketch.pde']);不起作用。我正在使用所有庫的當前版本。驚訝的是,我還沒有找到答案,因爲很多人似乎都有同樣的問題。謝謝你的幫助!在ajax請求後重新初始化processing.js草圖

索引頁:

$(document).ready(function() { 
    // put all your jQuery here. 

     //Check if url hash value exists (for bookmark) 
    $.history.init(pageload); 
     //highlight the selected link 
    $('a[href=' + document.location.hash + ']').addClass('selected'); 
     //Search for link with REL set to ajax 
    $('a[rel=ajax]').live("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 
     //$('#content').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=' + encodeURIComponent(document.location.hash); 
    $.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 
      $('#content').html(html); 

      //display the body with fadeIn transition 
      $('#content').fadeIn('fast'); 
      //reapply styles? 

      //apply syntax highlighting. this works 
      SyntaxHighlighter.highlight(); 
//relaod processing sketch, currently displays nothing 
      Processing.loadSketchFromSources($('#processing'), ['mysketch.pde']); 
     }  
    }); 
} 

這阿賈克斯負載的內容:

<!--ajax'd content--> 
    <??> 
    <h2>code</h2> 
    <pre class="brush: php"> 
    $last_modified = filemtime("header.php"); 
    echo("last modified: "); 
    echo(date("m.j.y h:ia", $last_modified)); 
    </pre> 
    <script type="application/processing"> 
    </script> 
    <canvas data-processing-sources="mysketch.pde" id="processing"> 
    </canvas> 
    </div> 

</body> 
</html> 
<??> 
+1

您現在可以添加您的代碼,否則答案將永遠不會來到其他地方...... –

+2

借調,仍然:如果您在Ajax請求之前運行了一些JS,然後向DOM添加了某些東西,顯然它不會受到先前的代碼。 – Viruzzo

+0

所以我想我的問題是我如何使.js再次火災?我可以自己弄清楚。 – expiredninja

回答

3

那麼,讓我們來分析一下,通常發生在你包括(外部或內部)的Javascript代碼:它會自動只執行全局範圍內可用的代碼。 「好的」腳本只會將一個命令添加到全局範圍,然後在函數/方法的某處執行初始化代碼。

您只需查看外部Javascript文件並查看全局範圍內正在執行的內容。沒有一般的答案......有些腳本使用一個對象並調用它的init()方法......但這完全取決於開發人員的想象力。

1

如果你的JavaScript需要觸發,則必須添加這頭元素:

var head = document.head || document.getElementsByTagName("head")[0]; 
var script = document.createElement("script"); 
script.innerHTML = "your AJAX-obtained js code"; 
head.appendChild(script); 

相同的技巧也適用於CSS。用CSS聲明將元素添加到頭部作爲innerHTML。所以:確保預處理您的AJAX響應並分離出JavaScript和CSS元素,然後將這些元素添加到文檔標題中。它可能更容易讓你的響應JSON對象的線沿線的:

{ 
    html: "<html>string<goes>here</goes></html>", 
    scripts: ["url1","url2","url2",...], 
    style: ... 
} 

,然後對其進行解析時JSON的HTML(您作爲innerHTML的使用新的使用document.createElement(「格」)什麼的,然後追加它需要追加的地方),腳本(你把它轉換成HEAD插入的元素)和樣式聲明(你把它變成HEAD插入的元素)。

(在功能音符,你的例子Ajax響應看起來有PHP代碼在裏面。我不知道你在使用它的是什麼,但是這看起來像一個壞響應)或許

+0

我很困惑。我的'響應'是一個PHP頁面,並不是說這個頁面當前與服務器交互,但我希望它有一天。也被你的「你的ajax獲得的代碼」的含義所迷惑,或者爲什麼我想要一個html字符串,我已經得到了正確的頁面。我也從來沒有搞錯JSON,這是否有必要?感謝你的幫助。 – expiredninja

+0

由於瀏覽器不「執行」PHP,因此您的響應中包含PHP代碼並沒有多大意義。像「$ var」和「echo」這樣的東西只是文本字符串,瀏覽器對它們什麼都不做,所以它們不會對任何與processing.js或相關的樣式有關。 如果您想要根據通過AJAX請求獲取的內容刷新processing.js和樣式,您必須得到正確的迴應:整齊分隔爲html(對於正文),css樣式(對於