开发者

document.getElementById returning element in firefox but not chrome

I am trying to dynamically load a coverflow type object (think itunes coverflow but less fancy) on a button click after the webpage has already loaded. I am dynamically creating a list of images to feed to the javascript for the coverflow that then loads them and does it's ma开发者_如何学运维gic to make it look pretty. The problem is that in firefox the code is working, but in chrome and ie (surprise surprise) the javascript is throwing an error and the page is going blank due to the error throw below. The problem is in this block of code that is trying to capture the dom element by id

if (typeof(this.Container) == 'string') { // no node
    var container = document.getElementById(this.Container);
    if (container) {
        this.Container = container;
    } else {
        throw ('ContentFlow ERROR: No element with id \''+this.Container+'\' found!');
        return;
    }
}

Pretty much in chrome the dome element is not captured by the code up above when it is definitely available in the page. I even typed out document.getElementById("container_name"); in the console of chrome and it returned the element so I am clueless as to whey it won't work in the file.

Any suggestions would be appreaciated

Thanks

EDIT:

here is the onpage html that is trying to be filled

<div id="contentFlow" class="cool_ui ContentFlow">
    <p id="coverflow_school_name"></p>
    <div class="loadIndicator"><div class="indicator"></div></div>
    <div class="flow" style="height:240px;"></div>
    <div class="scrollbar">
        <div class="slider"></div>
    </div>
</div>

ANSWERED:

Wow, ok. I figured it out. The problem was the way some resources were being dynamically loaded before the call to that piece of code. The resource loading was causing the page to go blank, thus making var container = document.getElementById(this.Container); return null hence the exception being thrown. Weird part was firefox could handle the resource loading, but chrome could not.

Thanks for the help


Why would you not use jquery?

 $('#container_name')

UPDATE: on second reading, is this the name or the Id of the element?

If it is name thenn use

$('[name="container_name"]')


Using JQuery you can just say

if($(this.Container).length){
    //Code goes here
}
else
{
    throw ('ContentFlow ERROR: No element with id \''+this.Container+'\' found!');
    return;
}


I tried the code in Chrome 12.0.742.112 and it seems to work fine. Here is a link: http://jsfiddle.net/eemYg/.
I presume another thing is causing the problem, for example this.Container. You should check it's value (in Chrome) and see if it has a different value than the one in Firefox.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜