Markcce Eros Markcce Eros - 1 month ago 6
Javascript Question

Javascript / jQuery change script src in body and refresh

I have a script which I need to change at runtime and reload changes

So the script is:

<body>
<script id="scriptId" src="myJsFile_1.js"></script>

//Rest of the page here


<script>

$(document).ready(function() {

$("#buttonId").click(function() {

//Change the script src to myJsFile_2.js and reload

})

});

</script>

</body>
</html>


How can I do this?

Answer

use this

$(document).ready(function() {
    $("#buttonId").click(function() {

        $(document.body).remove('#scriptId').append('<script id="scriptId" type="text/javascript" src="your_script_src"></script>');

    });
});