Elegant clean way to include HTML in JavaScript files?

后端 未结 7 2164
误落风尘
误落风尘 2020-12-23 10:05

I\'m building a small app with a few modal dialog windows. The windows require a tiny bit of HTML. I\'ve hard coded the window HTML in the javascript library but am not th

7条回答
  •  猫巷女王i
    2020-12-23 10:36

    There is 2 solutions tto your problem: - An alternative to the heredoc Syntax in javascript is to escape the newline char with \ :

    var tpl = "hello\
    stackoverflow\
    World !";
    

    The char is escaped so ignored, and it wont take place in the resulting string.

    You can also create a plain html file with your template, and in your js script you create a hidden iframe and load the crossdomain html template. You can now access the document object of the iframe and retreive body.innerHTML. In theory! I Didn't tested this solution yet....

提交回复
热议问题