Thiyagu
Thiyagu

Reputation: 786

Change Script tag value dynamically using javascript

I need to dynamcially change the script tag value based on user choice from drop down, find my code below, it does not return anything

function dynamic()
{
        var sel_size = jQuery("#block_size").val(); 
        var st = sel_size.split("x");
        var site_url = '<?php echo $site_url;?>';
        var path = '<?php echo $this->config->item("path");?>';
        var id = '<?php echo $zone_id;?>';
        var s = document.createElement("script");
            s.type = "text/javascript";
            s.src = url+path+"?id="+id+"width="+st[0]+"height="+st[1];
            s.innerHTML = null;
            alert(s);
}

What is wrong in this, please correct my code, Any help Appreciated...

Upvotes: 1

Views: 1417

Answers (4)

Versha Gupta
Versha Gupta

Reputation: 273

If url variable not present and have only site_url then use following code. Also added '&' to src.

 function dynamic(){
    var sel_size = jQuery("#block_size").val(); 
    var st = sel_size.split("x");
    var site_url = '<?php echo $site_url;?>';
    var path = '<?php echo $this->config->item("path");?>';
    var id = '<?php echo $zone_id;?>';
    var s = document.createElement("script");
        s.type = "text/javascript";
        s.src = site_url+path+"?id="+id+"&width="+st[0]+"&height="+st[1];
        s.innerHTML = null;
        alert(s);}

Upvotes: 0

Pasupathi Thangavel
Pasupathi Thangavel

Reputation: 942

In your function not having url variable. only having site_url variable.So try this one.And you are not put any return value on that function.

function dynamic()
{
    var sel_size = jQuery("#block_size").val(); 
    var st = sel_size.split("x");
    var site_url = '<?php echo $site_url;?>';
    var path = '<?php echo $this->config->item("path");?>';
    var id = '<?php echo $zone_id;?>';
    var s = document.createElement("script");
        s.type = "text/javascript";
        s.src = site_url+path+"?id="+id+"width="+st[0]+"height="+st[1];
        s.innerHTML = null;
        alert(s);
    return something;    // what you need to return in that one.
}

In this code may help you.

Upvotes: 0

Mohideen bin Mohammed
Mohideen bin Mohammed

Reputation: 20187

where is appendChild?

you have created dynamic script tag but you failed to append on body...

append it into body.

document.body.appendChild(s);

Upvotes: 0

FieryCat
FieryCat

Reputation: 1889

To apply script you have to run it:

function dynamic()
{
    // ... all other stuff
    var s = document.createElement("script");
    s.type = "text/javascript";
    s.src = url + path + "?id=" + id + "width=" + st[0] + "height=" + st[1];
    // Run your script
    document.body.appendChild(s);
}

Upvotes: 1

Related Questions