2014-07-01 50 views
-1

当我试图逃跑通过AJAX加载与下面的内容脚本标签:逃生脚本标记使用jQuery AJAX

$.ajax({ 
    cache: false, 
    type: 'GET', 
    url: 'index.html', 
    success: function(response) { 

     $(response).find('<script').replaceWith('\x3Cscript'); 
     $(response).find('</script>').replaceWith('\x3C/script>'); 

但是它给出了令牌是非法的错误...

我该如何解决这个问题?

回答

2

你应该从HTML5样板这个片段以正斜杠逃脱/,如:

<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.11.1.min.js"><\/script>')</script> 

像这样:

$(response).find('<script').replaceWith(function(){return '<script';}); 
$(response).find('</script>').replaceWith(function(){return '<\/script>';}); 
+0

仍然收到错误:'Uncaught SyntaxError:U非法的令牌ILLEGAL' – Cameron

+0

@Cameron哪一行是抛出错误?你能够在小提琴中重现这一点吗? – agconti

+0

第二个'$(response).find('').replaceWith('<\/script>');'你可以在这里看到错误:http://preview.na-software.co.uk/Demo/MarsPlaybook/ preview.html – Cameron

0

加载整个页面放入容器可以做到与​​,即

$('#iFrame').load('index.html', function() { 
    // page loaded into container 
    $('#iFrameLoading').fadeOut(function() { 
     $('#iFrame').fadeIn(); 
    }); 
}); 
+0

我需要脚本标记 – Cameron

+0

我把它写到一个iFrame看演示:http://preview.na-software.co.uk/Demo/MarsPlaybook/preview.html但脚本标记需要先转义。 – Cameron

+0

@Cameron你不能只用'$('#iFrame')。load('index.html');'? –