1

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...

2
  • 1
    where did you define url? in url+path Commented Jun 22, 2017 at 5:01
  • it will not return anything -- yes because return statement is missing. Commented Jun 22, 2017 at 5:01

4 Answers 4

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

where is appendChild?

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

append it into body.

document.body.appendChild(s);

Comments

0

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.

Comments

0

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);}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.