javascript in innerhtml not working - Stack Overflow

For example:<html><div id="media">123<div><a href="javascript:void

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

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

Share Improve this question edited Mar 15, 2013 at 4:59 user2171180 asked Mar 14, 2013 at 18:52 user2171180user2171180 191 silver badge5 bronze badges 1
  • Possible dupplicate of stackoverflow./questions/1068517/…. – antoyo Commented Mar 14, 2013 at 18:56
Add a ment  | 

3 Answers 3

Reset to default 7

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.

<script type="text/javascript">

function fun() {
    alert("working");
}
</script>

<div id="media">123</div> <a href="javascript:void(0)" onClick="fun();">Click</a>

Your JS function should be like this.

function fun() {
  alert("working");
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742342307a4425870.html

相关推荐

  • javascript in innerhtml not working - Stack Overflow

    For example:<html><div id="media">123<div><a href="javascript:void

    10小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信