1
I have that code in the database:
<script type="text/javascript">
alert("teste script-1");
var axel = Math.random() + "";
var a = axel * 10000000000000;
document.write('<iframe src="https:// .... ;ord=' + a + '?" width="1" height="1" frameborder="0" style="display:none"></iframe>');
</script>
<noscript>
<iframe src="https://...;ord=1?" width="1" height="1" frameborder="0" style="display:none"></iframe>
</noscript>
Responding to some events (page load or btn click), I have an AJAX function that goes to the database and recovers the script. See below:
$.getJSON( "returnScript.php", {objEvent: btn_id}, function() {
})
.done(function(dataset) {
//console.log(dataset);
//console.log(dataset.length);
if(dataset.length>0){
for (var index in dataset){
console.log(dataset[index].script);
$("body").append(dataset[index].script);
}
}
else{
console.log("sem script para ser executado");
}
});
In this code, after consulting the database, if there is a script to be executed (according to the criteria) it must recover all the code to make an append before finishing the body tag. As shown above, the code has a script part and an iframe part.
The fact is that if I paste this code into the page it will work well. But if I use $("body").append(dataset[index].script);
the alert("teste script-1");
performs correctly but the rest of the code makes the page all white (WSOD) with the following message in the browser console:
Uncaught TypeError: Cannot read property 'style' of null
at (index):551
at (index):551
at XMLHttpRequest.xhr.onreadystatechange ((index):551)
Clearly what is happening is that when the code is recovered by AJAX it does not work.
Someone would know how to fix this?
I am using Win7 + php5.3 + symfony.
Probably your problem is
document.write
in the recovered script via ajax. Try using a$('body').append('<iframe src="https:// .... ;ord=' + a + '?" width="1" height="1" frameborder="0" style="display:none"></iframe>');
.– rdleal
Correct. When we rotate
document.write
after the document has been loaded that fun overwrites all the code, but if it runs before the document is loaded it does not overwrite. As here in my case is an ajax, I have to replacedocument.write()
for$('body').append()
. http://stackoverflow.com/questions/19941866/document-write-overwriting-the-document . Post your reply to earn the point.– zwitterion
Exact. I waited for his return to find out if it was really the case, before explaining why this happens, since the comments have neither the purpose nor space for something more elaborate. I will answer.
– rdleal