Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

For example:

<html>
    <div id="media">123</div>
    <a href="javascript:void(0)" onClick="return fun()">Click</a>
    <script type="text/javascript">
        function fun() {
            document.getElementById("media").innerHTML = '<script type="text/javascript">alert("working");</script>';
        }
    </script>
</html>

After you click the alert does not show.

alert("working"); is just an example. I want it to finish one job other javascript . I have a job to be processed through ajax should have used innerHTML

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
226 views
Welcome To Ask or Share your Answers For Others

1 Answer

The HTML spec specifies that script tags inserted using innerHTML should not be executed. This is a security consideration.

There are still ways to do this if you are determined, such as adding it to img handlers or creating a script element, inserting it into the DOM and changing its text property. I will not elaborate on these, since doing this is generally considered somewhat sketchy. If you are not trying to inject script, you should include the script element in the page source.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...