Add doctype to an Iframe using javaScript/jQuery

408
November 23, 2016, at 4:01 PM

I want to add Doctype to an iframe once its loaded. I have tried the following script without any luck:

$(document).ready(function(){
   $('.ms-dlgFrame').ready(function(){
     var iframe = $('.ms-dlgFrame');
     var newHTML = "<!DOCTYPE html><html>"+iframe.contents().find('html').html()+"</html>";
     iframe.contents().find('html').html(newHTML);
     //iframe.contents().prepend('<!DOCTYPE html>');
   });
});

Seems its a simple thing but I am doing a mistake.

Answer 1

As far as I'm aware, you aren't able to target the contents of an iframe for security reasons.

Answer 2

I got the solution

$(document).ready(function(){
   $('.ms-dlgFrame').ready(function(){
     var iframe = $('.ms-dlgFrame');
     var newDoctype = document.implementation.createDocumentType(
        'html',
        '',
        ''
     );
     iframe[0].contentDocument.insertBefore(newDoctype,iframe[0].contentDocument.childNodes[0]);
   });
});
READ ALSO
Foundation 5: Styling my img size to the be 100% of a div

Foundation 5: Styling my img size to the be 100% of a div

I'm using Foundation 5 to set up a basic page. I'm trying to get a button to change the background-image of a certain div.

246
Need more css class in jquery split

Need more css class in jquery split

I need help with something about jquery split. .

327