I'm dynamically creating a iframe tag which pulls in content from page, how do I check if the page i'm pulling from has content before displaying it? should I be checking for the content first before generating the iframe tag?
How do I check if the iframe page is '<html><body></body></html>
', therefore don't show the 'adContainer'?
Heres some code I code that I've written so far...
function setAttributes(el, attrs) {
for(var key in attrs) {
el.setAttribute(key, attrs[key]);
}
}
// set ad iframe
function setAdiFrame(iWidth, iHeight, bookname) {
var randNum = Math.floor(Math.random()*1000000000);
var adFrame = document.createElement('IFRAME');
setAttributes(adFrame, {
"src": "http://www.foo.com/" + bookname + "/;cat=" + bookname + ";sz=" + iWidth + "x" + iHeight + ";ord=" + randNum,
"height": iHeight,
"width": iWidth,
"border": "0",
"scrolling": "no",
"allowtransparency": "true"
});
$('.ad').append(adFrame);
}
// ad container
var adContainer = $('.ad-container');
// check if ad container exist
if(adContainer.length > 0) {
// check if it's mobile
if(categorizr.isMobile) {
setAdiFrame(300, 400, 'bookname');
// otherwise, show desktop ad
} else {
setAdiFrame(728, 660, 'bookname');
}
// show ad once iframe page is loaded
adContainer.find('iframe').on('load', function() {
adContainer.css('display', 'block');
});
}
The outputted mark-up for adContainer is as such.
<div class="ad-container" style="display: block;">
<div class="ad">
<a id="close" href="button:close"></a>
<iframe src="http://www.foo.com;bookname/;cat=bookname;sz=728x660;ord=849890967" height="660" width="728" border="0" scrolling="no" allowtransparency="true"></iframe>
</div>
</div>
The outputted html within the src of the IFrame page could be '<html><head></head><body></body></html>
'. If that is the case, I do not want to display adContainer.