i want to make <script> ...... </script>
as text(or not executable code) which is returned from another page using ajax.
<div id="page"></div>
$.ajax({
url: "http://example.com",
type: "GET",
crossDomain: true,
dataType: "text/html",
success: function(response) {
console.log(response);
$('#page').append(response);
},
error: function(xhr, status) {
alert("error");
}
});
my problem is that the above code executes the script present inside the response
if error occurs my furthur page will not execute and program will stop. so i wanted to make script not executable
. how can i do it?
or how can i make any code inside script blank
suppose my script is like this
<script type="text/javascript">
// some erroneous code or some resource load which failed verification
console.log('i want to remove every code');
</script>
I think you have to do 3 steps
converting received htmlstring in to DOM element like
parser = new DOMParser() doc = parser.parseFromString(response, "text/xml");
or use
var doc = document.createElement('div');
doc.innerHTML = response;
Then finding all script tags init
allScriptTags = doc.find( "script" )
After that run loop on allScriptTags and remove all script tags
allScriptTags.forEach( function(item) { item.remove(); // or item.parentNode.removeChild(item); for older browsers (Edge-) });
Finally,
$.ajax({
url: "http://example.com",
type: "GET",
crossDomain: true,
dataType: "text/html",
success: function(response) {
console.log(response);
parser = new DOMParser()
doc = parser.parseFromString(response, "text/xml");
allScriptTags = doc.find( "script" )
allScriptTags.forEach(
function(item) {
item.remove();
// or item.parentNode.removeChild(item); for older browsers (Edge-)
});
$('#page').append(doc);
},
error: function(xhr, status) {
alert("error");
}
});
I hope it will help.
If it doesn't work, try to use string manipulation to remove tags from remove htmlstring.
Plunker : http://plnkr.co/edit/OUi8U945MIMFZoWQpBGy?p=preview
使用“<pre>”或(html5 support)标记按原样显示代码。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.