Scripts being stripped with jQuery .load

送分小仙女□ 提交于 2019-12-05 04:58:16

Like you said. jQuery extracts that element before executing only that block.

You could modify the output to have the server return the data in a format you need, or just write your own. It would extract the elements you need them output them to the DOM.

Untested, but it would be along the lines of this..

$('#content').load('loadTest.html #content,script');

From memory this more valid..

$.get('loadTest.html',function(r){
    var els = $(r).find('#content,script');
    $('#content').html(els);
});

After your comment I think you might be interested in something like js/css lazyload. It conditionally loads scripts, I'm using it on some smaller sites to load only scripts and CSS when I need them on different sub-pages or user-interaction.

You load it once in the head:

  <script src="js/lazyload-min.js" type="text/javascript"></script>

And then you can load scripts whenever you need them dynamically, one of the many examples:

// Load a single JavaScript file and execute a callback when it finishes.
LazyLoad.js('http://example.com/foo.js', function () {
  alert('foo.js has been loaded');
});

Works for CSS files, too.

This might be helpful in your case, however, it might change your loading logic as well. In any case I think normally you should have multiple AJAX endpoints on your server that will only return the data you ask for. Otherwise things can get specific and even tricky.

So more a comment than a real answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!