IE6 can't find Object element with getElementById ()?

I'm trying to get an object element from my webpage using getElementById (I can eventually replace it with a dynamically created object element), but it returns null

in IE6.

In the following code, the function byId()

returns null

in IE, but [object HTMLObjectElement]

in Firefox 3, and the function lengthOfByTagName()

returns 0

in IE, but 1

in Firefox.

Is there something I am doing wrong?

<html>
<head>
<title>IE doesn't see Object element?</title>
<script type="text/javascript">
function byId()
{
    var video = document.getElementById("VideoPlayer");
    alert(video);

}
function lengthOfByTagName()
{
    var length = document.getElementsByTagName("object").length;
    alert(length);

}
</script>

</head>
<body>
    <object type="" id="VideoPlayer">
        <param name="allowScriptAcess" value="always" />
        <param name="allowfullscreen" value="true" />
        VideoPlayer element
    </object>
    <br>
    <br>
    <a href="#" onclick="javascript:byId()">getElementById("VideoPlayer")</a>
    <br>
    <a href="#" onclick="javascript:lengthOfByTagName()">getElementsByTagName("object").length</a>
</body>
</html>

      

+1


source to share


3 answers


This has to do with the way IE handles <object> nodes versus the DOM.



Since you are dynamically changing anyway, I recommend that you instead create a <div> where you need it, and change innerHTML to HTML for the object you want.

+3


source


I just tested IE 7 and saw the behavior described

Internet Explorer does not expect free text in the tag <object>

. Using Debugbar, for example, you assumed that IE was not creating the correct DOM tree.

Use this code instead



<object type="" id="VideoPlayer">
    <param name="allowScriptAcess" value="always" />
    <param name="allowfullscreen" value="true" />
</object>

      

It will work as expected.

+3


source


In IE6, this may not work. For reference: https://msdn.microsoft.com/en-us/library/ie/ms536437%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396 log

Suggest jQuery to handle most cases

jQuery

var video = $("#VideoPlayer");

alert(video);

      

Try this in your coding.

0


source







All Articles