Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using $.getScript (jquery): code is not executed

I am working on updating my website, which now uses an AJAX engine. My engine works well, up-to-hand for some reason some pages do not execute javascript, let me explain: when the anchor change I use $.get for data recovery. The pages have this structure:

title
h1
script1.js,script2.js,etc.js
style1.css,style2.css,etc.css
<!--there's the page's content-->

It appears reload the page solves the problem, but i don't understand what is different. In the previous code, the engine runs successfully, reloaded or not:

$.getScript("script1.js");
$.getScript("script2.js");
$.getScript("etc.js");


In addition, a php generated script contains user's current state under an Object form:

$(function(){
    user = new Object();
    user.id = user.logged = <?php echo $user->getId();?>;
    user.nick = "<?php echo $user->getNick();?>";
    user.mail = "<?php echo $user->getMail();?>";

    user.logout = function(){

    };
});

The $.getScript request is successful, but the user object is not changed. The script, however, has yet been modified. And it don't works from console too.

The update is currently online at v2.wawolf.com, you'll find everything you need. Hotlink: Engine's code

like image 995
Pierre Tassel Avatar asked Jan 06 '15 05:01

Pierre Tassel


2 Answers

I just solved my problem: when all document is loaded, $(function(){}) will not work again, so calling a script which uses $(function(){}) will no get run. I removed it from all my secondary-called scripts and now all works perfectly!

like image 166
Pierre Tassel Avatar answered Oct 18 '22 08:10

Pierre Tassel


it might just be a loading order issue.

try encapsulating the JS loading in an onload function

$(window).load(function(){ 
    //get script loads here 
}

or

$(document).ready(function() {
   //get script loads here 
}

Sometimes I use one inside the other for dynamic JS script that needs to be loaded last.

like image 36
Paddy Avatar answered Oct 18 '22 09:10

Paddy