Markcce Eros Markcce Eros - 1 year ago 91
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:

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

//Rest of the page here


$(document).ready(function() {

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

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





How can I do this?

Answer Source

use this

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download