简体   繁体   English

在使用Ratchet \\ Push.js加载页面后执行自定义脚本

[英]Execute custom script after page loaded with Ratchet\Push.js

So on the GitHub documentation for Ratchet 2.0.2 I found the following statement. 因此,在Ratchet 2.0.2的GitHub文档中,我发现了以下声明。

Script tags containing JavaScript will not be executed on pages that are loaded with push.js. 包含JavaScript的脚本标记不会在使用push.js加载的页面上执行。 If you would like to attach event handlers to elements on other pages, document-level event delegation is a common solution. 如果要将事件处理程序附加到其他页面上的元素,则文档级事件委派是一种常见的解决方案。

Can someone please spell out exactly how to get a custom <script> to execute after being loaded by Push.js ? 有人可以详细说明如何在Push.js加载后获得自定义<script>执行吗?

On my first page, I have a Table view, with several links to other pages, one of them being a link to a second page with a Twitter Feed widget on it. 在我的第一页上,我有一个表视图,其中有几个指向其他页面的链接,其中一个链接指向第二个页面,其上有一个Twitter Feed小部件。

<li class="table-view-cell media">
    <a class="navigate-right" href="Twitter.php" data-transition="slide-in">
        <span class="media-object pull-left icon icon-person"></span>
        <div class="media-body">
            Twitter Feed
        </div>
    </a>
</li>

The second page only contains the twitter feed widget code. 第二页仅包含twitter feed小部件代码。 When I browse to this page directly (without being loaded by Push.js ) everything loads correctly, but when it is loaded via Push.js, the script is not executed. 当我直接浏览到这个页面(没有被Push.js加载)时,一切都正确加载,但是当它通过Push.js加载时,脚本不会被执行。

Can someone please explain what I need to do to get this script to execute after being loaded by Push.js ? 有人可以解释一下我需要做什么才能让这个脚本在被Push.js加载后执行吗? I've searched Google, Stack Exchange, and Github\\Ratchet issues and have not been able to find a good example of how to accomplish this. 我搜索了Google,Stack Exchange和Github \\ Ratchet问题,但未能找到如何实现这一目标的好例子。

One solution would be to add data-ignore="push" to the link, but I want to know how to do with WITH push.js . 一种解决方案是将data-ignore="push"到链接,但我想知道如何处理WITH push.js

<div class="content">
    <a class="twitter-timeline" href="https://twitter.com/XXXX" data-widget-id="XXXX">Tweets by XXX</a>
</div>
<script>
    !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
</script>

EDIT: below was how I originally solved this problem, which worked fine, but I came up with a better solution, which I posted as the answer to this question. 编辑:下面是我最初解决这个问题的方法,工作正常,但我提出了一个更好的解决方案,我发布了这个问题的答案


I finally figured it out. 我终于弄明白了。

On your first page , you need to do the following... 在您的第一页上 ,您需要执行以下操作...

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);

checkPage() will execute for every time a new page is loaded via push. checkPage()将在每次通过push加载新页面时执行。

Just made a change for Ratchet.js to make individual js works for each page more elegant.( https://github.com/mazong1123/ratchet-pro ) 刚刚对Ratchet.js进行了更改,使每个页面的单个js更加优雅。( https://github.com/mazong1123/ratchet-pro

By using the new ratchetPro.js, we can do followings: 通过使用新的ratchetPro.js,我们可以做到以下几点:

(function () {
    var rachetPageManager = new window.RATCHET.Class.PageManager();
    rachetPageManager.ready(function () {
       // Put your logic here.
    });
})();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM