Load partial html with javascript inside - Stack Overflow

On my website I load html, which is rendered at the server (nodejs), and insert it at the right positio

On my website I load html, which is rendered at the server (nodejs), and insert it at the right position (most time a div with id content).

How would I insert the received html on the client, so that included script tags are executed?

I am using on the client side underscore and handlebars. But vanillajs is also possible of course.

PS: Here is an example to show the difference between jQuery.html() and setting the innerHTML-property:

/

On my website I load html, which is rendered at the server (nodejs), and insert it at the right position (most time a div with id content).

How would I insert the received html on the client, so that included script tags are executed?

I am using on the client side underscore and handlebars. But vanillajs is also possible of course.

PS: Here is an example to show the difference between jQuery.html() and setting the innerHTML-property:

http://jsfiddle/waxolunist/VDYgU/3/

Share Improve this question edited May 23, 2013 at 4:51 Christian asked May 6, 2013 at 18:39 ChristianChristian 4,1243 gold badges29 silver badges49 bronze badges 4
  • You can refer stackoverflow./questions/8825804/… and res.Render in expressjs./api.html#req.xhr – DeveloperBuddy Commented May 6, 2013 at 18:45
  • Could you elaborate on this. I don't get it. – Christian Commented May 6, 2013 at 18:52
  • You can render html with a callback responding. res.render('sometemplate', {layout: false}); – DeveloperBuddy Commented May 6, 2013 at 19:06
  • But isn't res.render a serverside function, or how do I get the response object on the clientside? – Christian Commented May 6, 2013 at 19:35
Add a ment  | 

3 Answers 3

Reset to default 3

with jQuery

$('div#content').html(loaded_html);

without:

getElementById('content').innerHTML = loaded_html;

While possible to do it your way, a better idea might be to send json and render pages in the browser using the same templates you (possibly) use in the server. I'm using doT, which I think to be the best of all JavaScript based templates (like EJS, underscore).

You could leverage jQuery to do that. Just with

$.load("#divname").load(url)

http://api.jquery./load/

There is some problem with your character escaping, please use jshint. Otherwise it is pretty simple to use like

var script1 = "content for jQuery";
var script2 = "content for javascript";

$(document).ready(function (){
    $('#content1').html(script1);
    document.getElementById('content2').innerHTML = script2;
})

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

相关推荐

  • Load partial html with javascript inside - Stack Overflow

    On my website I load html, which is rendered at the server (nodejs), and insert it at the right positio

    7小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信