Using jQuery to change all instances of <object> to <iframe> for IE support
I am wondering if it is possible to use jQuery to switch out all <object> tags on a site for <iframe> tags when loading on IE 8 and below. In essence, this would allow the page to be XHTML 1.1 valid and not have to be double-coded all the time, eliminating the need to spend so much time focusing o开发者_如何学Cn non-standard browsers.
The data= attribute would need to be changed to src=, I'd like to insert frameborder="0", and all of the style values for the <object> tag would also need to be set for the <iframe> tag.
My goal here is not to create a debate on <iframe>s vs <object>s, I just think that this would be a huge tmie saver, and encourage proper, strict xhtml coding practices.
Thanks for any input!
For a basic rundown on how to change an element's type see jQuery convert DOM element to different type (no idea whether it'll work properly in XHTML, but it well might), but I don't think this is a good idea at all. I can see it cause delays on older machines (on which IE6 runs), and obviously, it won't work when JavaScript is disabled. All that to achieve valid code? Not a good idea IMO.
Before doing that, if the code has to be valid under any circumstances, I'd consider serving different code to IE6 on server side.
I do not know if you can change the DOM element type, but you can always create a new IFRAME element and delete the old element instead.
Something like (caution: untested!)
$("object").each(function() {
var obj = $(this);
$("<iframe/>").attr({
src: obj.attr("data"),
style: obj.attr("style"),
frameborder: 0
})
.after(obj);
obj.remove();
});
加载中,请稍侯......
精彩评论