2014-07-01 94 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');'? –