views:

2012

answers:

4
$('#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.

+3  A: 

Something like jQuery.getScript

$("#selector").click(function(){
  $.getScript("http://dev.jquery.com/view/trunk/plugins/color/jquery.color.js");
});
vsync
+2  A: 

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);
Cleiton
+1  A: 

A library that makes this work is LAB JS

andres descalzo
A: 

Is it possible to stop the script using the same technique ? (on a click event)

Johanna