jQuery .load() but exclude script tags

…衆ロ難τιáo~ 提交于 2019-12-11 01:19:30

问题


I am using the following jQuery to load in 5 pages into the current page

$('.main').load('main.cfm');
$('.bu1').load('bu1.cfm');
$('.bu2').load('bu2.cfm');
$('.bu3').load('bu3.cfm');
$('.bu4').load('bu4.cfm');

But each .cfm has the following script tags in them

            <script src="js/jquery.speedometer.js"></script> 
            <script src="js/jquery.jqcanvas-modified.js"></script> 
            <script src="js//excanvas-modified.js"></script> 
            <script>
            $(function(){
                $('#MDTQ').speedometer({
                    backgroundImage: "url(speedo/background-r.png)",
                    maximum: 15,
                    scale: 15,
                    suffix: ''
                });

                $('.changeSpeedometer').click(function(){
                    $('#MDTQ').speedometer({ percentage: $('.speedometer').val() || 0 });
                });
            });
            </script>

Is there a way to remove the below from the pages below whilst being loaded?

$('.bu1').load('bu1.cfm');
$('.bu2').load('bu2.cfm');
$('.bu3').load('bu3.cfm');
$('.bu4').load('bu4.cfm');

Thanks


回答1:


As load is a shortcut for $.get with the convenience of automagically inserting the content for you, you can use that and filter out the script tags before inserting the content yourself:

$.get('bu1.cfm', function(html) {
     var markup = $($.trim(html));
     markup.find('script').remove();

     $('.bu1').html(markup);
});

EDIT:

to filter by src:

$.get('bu1.cfm', function(html) {
     var markup = $($.trim(html));
     $('script[src]', markup).remove();
     $('.bu1').html(markup);
});



回答2:


use $.get with the relatively new $.parseHTML method.

$.get('bu1.cfm', function(html) {
    var markup = $.parseHTML(html);
    $('#bu1').html(markup);
});

by default $.parseHTML will remove any script tags within the markup.

If you instead want to only remove scripts that have a src attribute, use the 2nd attribute then filter the results.

$.get('bu1.cfm', function(html) {
    var markup = $($.parseHTML(html,true));
    var scripts = markup.find("script").addBack().filter("script");
    scripts.filter("[src]").remove();
    $('#bu1').html(markup).append(scripts);

});



回答3:


Not sure if using a regexp might be a consideration: Note: I used a div with a specific id instead of a class to test on. Also, my test file was a complete html document. I've not worked with cfm before, but perhaps the concept is the same.

$(function() {
    $.ajax({type: "GET",
        url: "yourfile.cfm", 
        dataType: "html",    
        error:function() { alert("error") },
        success:  function(data) {
            $("#main").html(data.replace(/<script[^>]*>[\w\W]*?<\/script>/g,""));
        }});
});


来源:https://stackoverflow.com/questions/17860446/jquery-load-but-exclude-script-tags

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