Executing several JS files with Ratchet push.js library

╄→尐↘猪︶ㄣ 提交于 2019-11-28 10:26:30
Schmalzy

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().

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.

I have found a solution for this problem when i'm working with push.js. What I ended up doing in the end is give each html page i want to load an #description. This means that when i link to the pages i link to contactlist.html#contact.

On my main html page, i add the event listener:

 window.addEventListener('push', pageHandler);

Then in a separate file i run this script.

var pageHandler= function(){
  var page= window.location.hash;

  if (page == '#contact'){
     //Put every script you want to use on this page here. 
  }
  if(page == '#info'){
    //Same here, every script you need for this page. 
  }
};

Don't know if this is the best way, but i feel like I then have good control over my script, and it works.

Just put my thoughts here. I made a change for Ratchet.js to make individual js works for each page. Each html has a js file linked. (https://github.com/mazong1123/ratchet-pro)

By using the new ratchetPro.js, we can do followings:

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

Call window.RATCHET.getScript to load your external js. Once the external js has been loaded, it'll be cached and never load again. However, the callback will always be executed when the page switched in.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!