2

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.

1
  • I think, if you change the doctype on the fly, it won't change the way how the document is rendered. Maybe the doctype should already be set in the src of the iframe before changing its HTML? Commented Oct 28, 2015 at 14:08

2 Answers 2

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]);
   });
});
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

This is only true if you don't control the target.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.