最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - document.getElementById() returns null on IE9 - Stack Overflow

programmeradmin3浏览0评论

I'm working on a popup and i'm having some hard time with Internet Explorer 9. This is the piece of code that gives me trouble:

var popUp= document.getElementById('projectInfo');
popUp.style.left=(tempX-310)+'px';
popUp.style.top=(tempY-110)+'px';

In IE9 (haven't tested in prior versions), popup is null. In adition, i've tried including my .js file just before the body closing tag and wrapping my function in a "document.ready()" function but none of this worked. The same code though works perfectly in Opera, Chrome and Firefox. Does anyone know what's going on?

Note: The function is called in the body's onLoad atribute of my html.

I'm working on a popup and i'm having some hard time with Internet Explorer 9. This is the piece of code that gives me trouble:

var popUp= document.getElementById('projectInfo');
popUp.style.left=(tempX-310)+'px';
popUp.style.top=(tempY-110)+'px';

In IE9 (haven't tested in prior versions), popup is null. In adition, i've tried including my .js file just before the body closing tag and wrapping my function in a "document.ready()" function but none of this worked. The same code though works perfectly in Opera, Chrome and Firefox. Does anyone know what's going on?

Note: The function is called in the body's onLoad atribute of my html.

Share Improve this question asked Jan 24, 2012 at 2:24 Francisco RagoutFrancisco Ragout 1031 gold badge1 silver badge4 bronze badges 6
  • 6 Can you include the HTML? – SoWeLie Commented Jan 24, 2012 at 2:26
  • These links seem relevant: - social.msdn.microsoft./Forums/zh/iewebdevelopment/thread/… - code.google./p/svgweb/issues/detail?id=616 Are you using any non-standard HTML attributes in your HTML - or possibly something like svgweb? – ziesemer Commented Jan 24, 2012 at 2:33
  • 1 @Trevor That should be a TypeError: Cannot call method 'getElementById' of null error. – alex Commented Jan 24, 2012 at 2:38
  • Hi Ziesemer, thank you for your time. I'm not using svgweb nor non-standar HTML attributes. Just this simple div <div id='projectInfo' class='popUp' style='left:200px; top:100px;'> – Francisco Ragout Commented Jan 24, 2012 at 3:17
  • 1 Any chance you have two elements with same id projectInfo? – Salman Arshad Commented Mar 3, 2012 at 10:02
 |  Show 1 more ment

3 Answers 3

Reset to default 1

Without using function it can't work

 window.onload = function() {
   var popUp= document.getElementById('projectInfo');
   popUp.style.left=(tempX-310)+'px';
   popUp.style.top=(tempY-110)+'px';
 }

IE is having some known issues with getElementById.This post may help .

  1. http://webbugtrack.blogspot./2007/08/bug-152-getelementbyid-returns.html

  2. http://www.impressivewebs./avoiding-problems-with-javascript-getelementbyid-method-in-internet-explorer-7/

In previous versions of IE (and apparently Chrome and Firefox), getElementById would check for an object with the given id and if it didn't find it, it would take an element with that name.

IE9 doesn't do this, so you need to make sure you have an element with id = projectInfo, not just name=projectInfo. We just discovered this throughout one of our applications. Not great.

发布评论

评论列表(0)

  1. 暂无评论