How to change external script url onclick?

I struggled to find a solution to my problem. I'm not a javascript specialist, but I'm looking for a way to change the external js url depending on the onclick event to ahref.

Now I have this in my tags:

<script type="text/javascript" src="http://www.domain.com/loader.php?GID=126&go=&sid="></script>

I want the SID parameter to be set to 1 or 2 based onclick on the anchor tag. The bootloader is called using this piece of code, but the SID would need to change the right to that called by the bootloader.

<a href="/#download" onclick="javascript:Load_126(); return false;">

Is there anyone who could tell me if this is possible? And maybe point me in the right direction?

Hi,

Sedoc94

EDIT 1 : I pointed in the direction and decided that I can do it using jQuery.getScript() but still don't know how I should use it for my case.

EDIT 2 : as the script needs to be pulled out of the external domain, I will have to go with the $ .ajax () function.

Now I have:

 function loadGame(sid){ $.ajax({ url: 'http://www.domain.com/loader.php?GID=126&go=&sid='+sid, dataType: "script", crossDomain: true, success: gLoad_12603() }); } 

With ahref onclick, I call the loadGame function, but the console says: Uncaught ReferenceError: gLoad_12603 is not defined . It should work. But I guess it somehow gives this error, because the function exists only in the script code, which is returned from an external URL.

Any ideas how I can make it work?

+7
source share
3 answers

You need to provide a success callback that will be executed after the AJAX request completes. Right now, you are actually saying gLoad_12603 () with a parathesis and executing the function before the AJAX request even started. You cannot just remove the parathesis, because jQuery may try to get a link to the gLoad_12603 function before the AJAX request is also triggered. So you have to wrap your call to gLoad_12603 () in a function to ensure that everything works out:

 function loadGame(sid){ $.ajax({ url: 'http://www.domain.com/loader.php?GID=126&go=&sid='+sid, dataType: "script", crossDomain: true, success: function() { gLoad_12603(); } }); } 
+2
source

jQuery.getScript()

 function loadScript(sid) { $.getScript("http://www.domain.com/loader.php?GID=126&go=&sid="+sid, function(script, textStatus, jqxhr) { //code you want to be executed after the script is loaded inside the page //you can delete this callback if you don't need it }); } 

And use it this way by changing the sid on each anchor to load different scripts:

 <a href="/#download" onclick="javascript:loadScript(126); return false;"> <a href="/#download" onclick="javascript:loadScript(127); return false;"> 
+1
source

Try removing the bracket "()" in "success: gLoad_12603 ()".

+1
source

All Articles