Loading jQuery script on click event

$('#selector').click(function() {
    // here I would like to load a javascript file
    // let say /js/script-on-click-event.js
});

How is this possible? I'm not sure, but I remember reading about the function that does this in the jQuery documentation, but I can’t find it here, maybe it is deprecated or I saw it elsewhere?

Basically, I want to load the script into the click event.

+3
source share
3 answers

Something like jQuery.getScript

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

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);
+8
source

The library that does this work, LAB JS

+1
source

Source: https://habr.com/ru/post/1716710/


All Articles