2011-12-01 67 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样式(对于