Why doesn't embedded JavaScript execute when inserted in response to an ajax call?

后端 未结 2 363
北荒
北荒 2021-01-26 17:20

Inserting JavaScript in the .innerHTML does not cause it to run.

I have what I call a mini-feed in which a user submits a post. This initiates an ajax call which saves

2条回答
  •  没有蜡笔的小新
    2021-01-26 17:52

    The following code should work:

    function onSuccess() {
        src = document.createElement('script');
        src.innerHTML = this.responseText;
        document.body.appendChild(src);
    }
    

    But I would agree with cwallenpoole that why not have all that javascript code (for pretty-fying the date) be already written in advanced in the main page? (or script included or something).

提交回复
热议问题