Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery doesn't recognize function when it's added later
    text
    copied!<p>Below is a snippet of my JS</p> <p>It loads JS functions if PHP asks for them. But for my question this isn't really important.</p> <p>Almost at the bottom of my example you'll find: <code>loadjscssfile("js/functions/"+data.action+".js", "js");</code> What it does is this: let's say <code>data.action = 'helloworld'</code> it will load the file: js/functions/helloworld.js AND it will check for the function <code>helloworld()</code> The problem is in the last part, the helloworld.js gets loaded. But when I do a: <code>$.isFunction('helloworld')</code> it doesn't work.</p> <p><strong>UPDATE: The jQuery function AJAX does the trick, below also the solution</strong></p> <pre><code>// // LOAD JS OR CSS // var fileref; function loadjscssfile(filename, filetype){ if (filetype=="js"){ //if filename is a external JavaScript file fileref=document.createElement('script'); fileref.setAttribute("type","text/javascript"); fileref.setAttribute("src", filename); } else if (filetype=="css"){ //if filename is an external CSS file fileref=document.createElement("link"); fileref.setAttribute("rel", "stylesheet"); fileref.setAttribute("type", "text/css"); fileref.setAttribute("href", filename); } if (typeof fileref!="undefined"){ document.getElementsByTagName("head")[0].appendChild(fileref); } } // // SET INIT AJAX FUNCTION // function init(i){ $.ajax({ dataType: "json", url: "ajax.php?i="+i }).done(function( data ) { if(data.menu!='N.U.'){ $('.menu').html(data.menu); } if(data.container!='N.U.'){ $('.container').html(data.container); } if(data.action!='N.U.'){ if(data.action_val!='N.U.'){ var funcCall = data.action + "('" + data.action_var + "');"; } else { var funcCall = data.action + "();"; } if($.isFunction(funcCall)){ eval(funcCall); } else { // // function doesnt excist try to load function from dir // loadjscssfile("js/functions/"+data.action+".js", "js"); // // try again // if($.isFunction(funcCall)){ eval(funcCall); } else { //alert('FATAL ERROR: JS function ('+data.action+') doesnt excist'); } } } }).fail(function(){ alert( "error" ); }); } </code></pre> <p><strong>SOLUTION</strong>:</p> <pre><code>// // SET INIT AJAX FUNCTION // function init(i){ $.ajax({ dataType: "json", url: "ajax.php?i="+i }).done(function( data ) { if(data.menu!='N.U.'){ $('.menu').html(data.menu); } if(data.container!='N.U.'){ $('.container').html(data.container); } if(data.action!='N.U.'){ if(data.action_val!='N.U.'){ var funcCall = data.action + "('" + data.action_var + "');"; } else { var funcCall = data.action + "();"; } if (typeof window[data.action] === "function") { eval(funcCall); } else { // // function doesnt excist try to load function from dir // $.ajax({ url: "js/functions/"+data.action+".js", dataType: "script" }).done(function(){ // // try again // if (typeof window[data.action] === "function") { eval(funcCall); } else { alert('FATAL ERROR: JS function ('+data.action+') doesnt excist'); } }); } } }).fail(function(){ alert( "error" ); }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload