Formatting this JavaScript Line

前端 未结 6 1014
盖世英雄少女心
盖世英雄少女心 2020-12-22 01:02

I am trying to format this line of code in my popup window, but i am facing unterminated string literal error.

Can somebody please tell me how best I co

6条回答
  •  失恋的感觉
    2020-12-22 01:12

    Best not to use a string, but an anonymous function instead:

    window.setTimeout(function () {
        winId.document.write(
          '\n'
        );
    }, 10);
    

    Using strings in setTimeout and setInterval is closely related to eval(), and should only be used in rare cases. See http://dev.opera.com/articles/view/efficient-javascript/?page=2

    It might also be worth noting that document.write() will not work correctly on an already parsed document. Different browsers will give different results, most will clear the contents. The alternative is to add the script using the DOM:

    window.setTimeout(function () {
        var winDoc = winId.document;
        var sEl = winDoc.createElement("script");
        sEl.src = "../js/tiny_mce/tiny_mce.js";
        winDoc.getElementsByTagName("head")[0].appendChild(sEL);
    }, 10);
    

提交回复
热议问题