0

Here is my test javascript function

<script type="text/javascript">
    function test() {
        var sHTML = "<script type='text/javascript'> " +
                        "function showmsg() { " +
                            "alert('message'); " +
                        "} " +
                     "<\/script>" +
                     "<div>" +
                        "<a href='#' onclick='javascript:showmsg(); return false;'>click</a>" +
                     "</div>";
        var divTemp = document.createElement("div");
        divTemp.innerHTML = sHTML;

        var d = document.getElementById("div1");
        d.appendChild(divTemp);
    }
</script>

When I run this function, the div along with a tag is added in the div1, but when I click on anchor tag, it says showmsg is not defined, which is indicating that browser is NOT parsing the script tag.

How to achieve this without any 3rd party library?

Update:

The possible usage is, I want to allow user to create HTML templates along with JavaScript code, then my JS library will use those HTML templates to render user defined markup, plus allowing user to implement his/her custom logics through JS.

3 Answers 3

1

You need to run eval on the script contents when using innerHTML. Try something like:

var scripts = divTemp.getElementsByTagName('script');
for(var i=0; i<scripts.length; i++) {
    eval(scripts[i].textContent);
}

Obviously, you need to do this in end, after injecting the innerHTML into the DOM.

Sign up to request clarification or add additional context in comments.

3 Comments

@sallushan Yes it is: jsbin.com/huqaz/1/edit?html,js,output (I changed to textContent for FF support)
Yes, its working fine on jsbin, but when I ran it through my test web page in Chrome and IE, its NOT working. However if I run eval() in console, it creates the function.
OK got it working, actually it was the context issue, I used it as, eval.call(window, scripts[i].textContent). Thanks.
1

Browsers do not run JavaScript code when a <script> tag is dynamically inserted like that.

Instead of that, you can just define the function directly!

    function test() {
      window.showmsg = function() {
        alert("message");
      };

      var sHTML = "<div>" +
                    "<a href='#' onclick='javascript:showmsg(); return false;'>click</a>" +
                 "</div>";
    var divTemp = document.createElement("div");
    divTemp.innerHTML = sHTML;

    var d = document.getElementById("div1");
    d.appendChild(divTemp);
}

Libraries like jQuery have code to strip <script> blocks out of text that's being stuffed into some element's innerHTML, and then evaluate it with eval(), so that's one thing to do if the code is somehow "stuck" in a block of content.

2 Comments

yes obviously, but the problem is I have this thing in string, so can't define the function
@sallushan well if you could provide more context it might be possible to offer specific suggestions. As I said in my answer, some DOM manipulation libraries provide this service. If you don't want to use such a library, you can just steal code from one.
0

Using jquery, is this can help you :

$('<script>function test(){alert("message");};</' + 'script><div><a href="#" onclick="test();">click</a></div>').appendTo(document.body)

http://jsfiddle.net/TSWsF/

1 Comment

thanks zeomega, trying to avoid any 3rd party library

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.