使用 Ratchet push.js 库执行多个 JS 文件 [英] Executing several JS files with Ratchet push.js library
问题描述
我正在使用 Ratchet 2.0.2 开发 Phonegap 应用程序,使用 push.js 在页面之间进行转换.一切都很顺利,但几个小时前我偶然发现了这一点:
<块引用>包含 JavaScript 的脚本标签不会在页面上执行加载了 push.js.如果您想附加事件其他页面元素的处理程序,文档级事件委托是一种常见的解决方案.
在做了更多研究之后,我发现了这一点:使用 RatchetPush.js 加载页面后执行自定义脚本
这几乎与我遇到的问题相同,但是我需要更进一步,因为我不仅需要加载一个脚本,而且需要加载两个(将来可能还会加载更多),我如何利用 checkPage() 加载不止一个脚本?
var checkPage = function(){//仅当twitter-widget存在于页面上时才运行if(document.getElementById('twitter-widget')) {loadTwitterFeed(document,"script","twitter-wjs");}};window.addEventListener('push', checkPage);
UPDATE:另见 这个问题.
这是我尝试过的一个解决方案,它似乎可以正常工作,但我正在寻找一些关于这是否是最佳方法的反馈......请随时发表评论,我将进行更新需要.我知道 eval()
很危险,但我不确定在这种情况下它有多危险.
在您的索引第一页上,将以下脚本放在页面底部,.content
div 的OUTSIDE,这样它就不会被 <代码>push.js.
然后,在通过 push.js
加载的任何其他页面上,只需给脚本一个 .js-custom
类.这个脚本必须放在INSIDE .content
div.
...<script class="js-custom">alert('我被处决了!');
第一个脚本将使用 .js-custom
类查找并遍历任何 标签,并使用
eval()<执行它的内容/代码>.
I´m developing a Phonegap app with Ratchet 2.0.2 using push.js for transition between pages. Everything is working smoothly but a couple of hours ago I stumble upon this:
Script tags containing JavaScript will not be executed on pages that are loaded with push.js. If you would like to attach event handlers to elements on other pages, document-level event delegation is a common solution.
After doing more research I found out this: Execute custom script after page loaded with RatchetPush.js
Which is almost the same problem I have, however I need to go further because I need to load not only one script, but two (and perhaps more in the future), how can I take advantage of checkPage() to load more than one script?
var checkPage = function(){
//Only run if twitter-widget exists on page
if(document.getElementById('twitter-widget')) {
loadTwitterFeed(document,"script","twitter-wjs");
}
};
window.addEventListener('push', checkPage);
UPDATE: Also see this question.
Here is a solution that I have tried and it seems to work correctly, but I'm looking for some feedback on if this is the best method or not... please feel free to comment, and I will make updates as needed. I know that eval()
is dangerous, but I' not sure how dangerous it is in this case.
On your indexfirst page place the following script at the bottom of your page, OUTSIDE of the .content
div, so that it does not get overwritten by push.js
.
<script>
window.addEventListener('push', function(){
var scriptsList = document.querySelectorAll('script.js-custom');
for(var i = 0; i < scriptsList.length; ++i) {
eval(scriptsList[i].innerHTML);
}
});
</script>
Then, on any other page that gets loaded via push.js
, just give the script a class of .js-custom
. This script must be placed INSIDE the .content
div.
<div class="content">
...
<script class="js-custom">
alert('I was executed!');
</script>
</div>
The fist script will find and loop through any <script>
tags with the .js-custom
class and execute it's contents using eval()
.
这篇关于使用 Ratchet push.js 库执行多个 JS 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!