Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamically change script src client-side

I have an HTML page which contains:

  • a <select> dropdown with 3 options with an onchange event to a JS function
  • a DIV holder with a script element inside:

Here's the code:

<select onChange="getData(this.value);">
    <option value="-">Limba/Language</option>
    <option value="ro">Romana</option>
    <option value="en">Engleza</option>
</select>
<div id="output">
    <script id="widget" type="text/javascript" src="js1.js"></script>
</div>

And here's the JS function:

<script type="text/javascript">
function getData(title)
{
    switch(title)
    {
        case "ro":
            var s = document.createElement("script");
                s.type = "text/javascript";
                s.src = "js1.js";
                s.innerHTML = null;
                s.id = "widget";
                document.getElementById("output").innerHTML = s;
        break;
        case "en":
            var s = document.createElement("script");
                s.type = "text/javascript";
                s.src = "js2.js";
                s.innerHTML = null;
                s.id = "widget";
                document.getElementById("output").innerHTML = s;
        break;
        default:
        void(0);
    }
}
</script>

When I select option 2 or 3 I get this output in my browser: [object HTMLScriptElement]

What I want to do is switch the src property of the script tag based on the value selected in the dropdown element. I want to do this client-side and preferably without any external libraries...

like image 320
Bogdan Avatar asked Dec 06 '12 16:12

Bogdan


People also ask

Does script src go in head or body?

The <script> TagScripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

How to load third party scripts dynamically in JavaScript?

We start by creating an empty <script></script> tag in memory as script and then assign the necessary attributes to it src (the URL where the script lives) and the id to identify the script later. Finally, we append the script to our <body></body> tag to actually load it.

Where should script src go?

The script tag should always be used before the body close or at the bottom in HTML file. The Page will load with HTML and CSS and later JavaScript will load.


2 Answers

The other solution is great :) ...but if you still want to create elements via JavaScript you should use appendChild instead of innerHTML...

Here is what your JavaScript should be :)

<script type="text/javascript">
function getData(title)
{
    switch(title)
    {
        case "ro":
            var s = document.createElement("script");
                s.type = "text/javascript";
                s.src = "js1.js";
                s.innerHTML = null;
                s.id = "widget";
                document.getElementById("output").innerHTML = "";
                document.getElementById("output").appendChild(s);
        break;
        case "en":
            var s = document.createElement("script");
                s.type = "text/javascript";
                s.src = "js2.js";
                s.innerHTML = null;
                s.id = "widget";
                document.getElementById("output").innerHTML = "";
                document.getElementById("output").appendChild(s);
        break;
        default:
        void(0);
    }
}
</script>
like image 184
Joseph Avatar answered Sep 24 '22 07:09

Joseph


function getData(title){
    document.getElementById("widget").src= (title == "ro" || title == "-") ? "js1.js" : "js2.js";
}
like image 25
bluetoft Avatar answered Sep 25 '22 07:09

bluetoft