1

I dont believe this can be done, but is it possible to alter the content of an iframe that is rendered via a src.

The 3rd party compiled ASP.Net control (Telerik RadEditor .Net 2 version) I use has an iframe in part of its rendered code and does not contain a doctype and it is causing problems in IE8 with certain elements.

As it is compiled, I can not add it in the source. I was wondering if it is possible to add it in another way?

I have tried multiple things in jquery such as:

$(element).html().prepend("doc type here");

$(element).html("doctype here" + $(element).html()); 

and all other kinds of dodgy work.

Cyassin
  • 1,437
  • 1
  • 15
  • 31

2 Answers2

1

By src, you mean content, right? (src is an attribute of an iframe which specifies the URI of the iframe itself)

You may change the doctype by changing the content of an iframe. One possible way is to recreate an iframe and reinsert the content, while prepending a doctype. Unfortunately, it is not possible to prepend a doctype to an existent iframe using DOM.

Example:

var iframe = document.createElement("iframe");
var idocument = iframe.contentDocument;
idocument.open();
idocument.write("<!DOCTYPE html...") // Doctype goes here
idocument.write("<html>...</html>") // Content goes here
idocument.close();
Community
  • 1
  • 1
Arseni Mourzenko
  • 50,338
  • 35
  • 112
  • 199
  • Yeah thats what I mean, the url of the iframe has come from another page, so in my head my thought's are you cant change its content? I will try your example – Cyassin May 28 '14 at 03:06
  • @Nick: I apologize, Nick, I originally misread your question. I've edited my answer in order to focus on the doctype issue. – Arseni Mourzenko May 28 '14 at 03:12
  • Thanks, this works to a degree, but it appears the 3rd party control is doing something funky with it. I'll just play with it a bit and let you know how it goes. – Cyassin May 28 '14 at 03:23
  • It is currently rendering like – Cyassin May 28 '14 at 03:31
  • How would this work for an existing iframe, instead of a new one? – Cyassin May 28 '14 at 03:38
  • @Nick: you have to create an iframe. Unfortunately, [it is not possible](http://stackoverflow.com/a/7159267/240613) to prepend a doctype to an existent iframe using DOM. – Arseni Mourzenko May 28 '14 at 03:41
  • Oh Okay. That was the specific question I had asked at the start. – Cyassin May 28 '14 at 03:42
1

I did domethin similar using iframes, using jQuery: $('iframe').contents().find("#internaldiv"), it works, but only if the iframe src and the main website are part of the same domain, in other cases you have to deal with the same domain policy (http://en.wikipedia.org/wiki/Same-origin_policy) and implement you cross domain policy in your server and in the other domain server.

soloidx
  • 729
  • 6
  • 12
  • Thanks, a previous answer identified how to do it, first, but thanks for mentioning the same domain policy. In this instance it was correct. – Cyassin May 28 '14 at 03:27