$('#selector').click(function() {
// here I would like to load a javascript file
// let's say /js/script-on-click-event.js
});
Is that somehow possible? I'm not sure but I remember reading about a function that does this in JQuery documentation but I cannot find it here, maybe it's been deprecated or I saw it elsewhere?
Basically what I want is to load a script on a click event.
$('#selector').click(function() {
// here I would like to load a javascript file
// let's say /js/script-on-click-event.js
});
Is that somehow possible? I'm not sure but I remember reading about a function that does this in JQuery documentation but I cannot find it here, maybe it's been deprecated or I saw it elsewhere?
Basically what I want is to load a script on a click event.
Share Improve this question edited Apr 26, 2018 at 10:09 vsync 130k59 gold badges340 silver badges421 bronze badges asked Sep 2, 2009 at 17:13 Richard KnopRichard Knop 83.7k154 gold badges398 silver badges560 bronze badges3 Answers
Reset to default 9Something like jQuery.getScript
$("#selector").click(function(){
$.getScript("http://dev.jquery.com/view/trunk/plugins/color/jquery.color.js");
});
You can do the same using only javascript:
var script = document.createElement('script');
script.src = 'http://somesite.com/somescript.js';
script.type = 'text/javascript';
document.body.parentNode.appendChild(script);
A library that makes this work is LAB JS