1. Home
  2. Computing & Technology
  3. JavaScript

Internet Explorer and Objects

Join the Discussion

Questions? Comments?

After losing a court case Microsoft have been forced to change the way that Internet Explorer processes embedded objects. Instead of them being able to place multimedia into object tags in your HTML and have them play seamlessly as part of the page Internet Explorer now has to ask for permission before allowing the object to run.

If you have web pages with objects in them and you don't want Internet Explorer to pop up this alert before running your object then you need to change your web page so that the object is added via Javascript rather than having it hard coded in the HTML or (easier) to have Javascript re-add the code in place of itself in the HTML to avoid the problem.

Adding the following code to any external Javascript that you already have linked into the head of each web page will resolve the problem for all objects on your site in one go:

function obfix() {
if (document.body.outerHTML &&
(ob = document.getElementsByTagName('object')).length) {
for (var i=ob.length-1; i >= 0; i--) {
ob[i].outerHTML = ob[i].outerHTML;}}}
window.onload=obfix;

The code performs the calculations in the order that it does in order to minimize the impact on pages where it is not required and to perform as efficiently as possible on pages where it is required. All browsers except Internet Explorer and Opera don't understand outerHTML so the first test skips the rest of the code for all other browsers (unfortunately the rest of the code runs unnecessarily for Opera but adding a test to skip over it would only be more efficient in Opera when there are several objects on the page and it would then always be less efficient for Internet Explorer). The second test skips over the rest of the processing if there are no objects on the page as well as assigning the objects to an array called ob when there are (that single = is not a typo so don't change it to == as that will stop the code working). We then process backwards through the objects on the page since it doesn't matter which order we process them in and doing it backwards means we don't need to test the length of the array each time around the loop.

The only issue that you might have in adding this code is if your page(s) already have a function attached to the onload event handler. If that is the case then just remove the last line from this code and add obfix(); to the end of the function that the onload event already calls instead.

Explore JavaScript
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.