Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing several JS files with Ratchet push.js library

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 Ratchet\Push.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);
like image 665
Danito Avatar asked Apr 21 '14 23:04

Danito


2 Answers

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 index\first 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().

like image 88
Schmalzy Avatar answered Oct 02 '22 03:10

Schmalzy


Here is my version of loading scripts for push.js, adapted from Danito's answer.

I have a 1:1 relationship for html and js files. There will be a thinga.html and thinga.js file. What I decided to do was to add an id of thinga to the content div in the html, like so:

<div class="content" id="thinga">
    ...
</div>

Then, in the initial app setup I attached a function called 'checkPage' to the push global event:

window.addEventListener('push', checkPage);

The checkPage function grabs the id from the content div and checks to see if there a javascript file of the same name, and if so, load it using jQuery's $.getScript() from the scripts/ directory

var checkPage = function(){

    var elm = document.getElementsByClassName("content")[0];
    var script = elm.id;

    if(script) {
        $.getScript("scripts/"+elm.id+".js")
        .done(function( script, textStatus ) {
           console.log( textStatus );
        })
        .fail(function( jqxhr, statusText, errorThrown ) {
            console.log(errorThrown);
            console.log(statusText);
            console.log(jqxhr);
        });
    }
};

This seems to do the trick, at least for my use-case. Just wanted to throw it out there on the off chance that it may help someone else someday.

like image 35
TheBrockEllis Avatar answered Oct 02 '22 03:10

TheBrockEllis