Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery load script on event (click)

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

like image 784
Richard Knop Avatar asked Sep 02 '09 17:09

Richard Knop


3 Answers

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);
like image 109
Cleiton Avatar answered Sep 22 '22 15:09

Cleiton


Something like jQuery.getScript

$("#selector").click(function(){
  $.getScript("http://dev.jquery.com/view/trunk/plugins/color/jquery.color.js");
});
like image 40
vsync Avatar answered Sep 20 '22 15:09

vsync


A library that makes this work is LAB JS

like image 23
andres descalzo Avatar answered Sep 19 '22 15:09

andres descalzo