admin管理员组文章数量:1391784
I am currently facing an error in JavaScript. The code is given below
function loader() {
size = window.innerWidth;
size = size - 300;
mainj = document.getElementById('main1');
mainj.style.left = size.toString() + 'px';
submainj = document.getElementById('submain1');
submainj.style.left = size.toString() + 'px';
size = mainj.style.top + 26;
document.getElementById('submain1').style.top = size.toString() + 'px';
}
onload = loader();
The error es only in Internet Explorer and the code works perfectly in Firefox. The error shows is in the fifth line, the error being
Message: Object required
Line: 34
Char: 1
Code: 0
URI: http://localhost/home.php
Line 34 is the 5th line given in the code - 'mainj.style.left.....' How can I fix this problem?
I am currently facing an error in JavaScript. The code is given below
function loader() {
size = window.innerWidth;
size = size - 300;
mainj = document.getElementById('main1');
mainj.style.left = size.toString() + 'px';
submainj = document.getElementById('submain1');
submainj.style.left = size.toString() + 'px';
size = mainj.style.top + 26;
document.getElementById('submain1').style.top = size.toString() + 'px';
}
onload = loader();
The error es only in Internet Explorer and the code works perfectly in Firefox. The error shows is in the fifth line, the error being
Message: Object required
Line: 34
Char: 1
Code: 0
URI: http://localhost/home.php
Line 34 is the 5th line given in the code - 'mainj.style.left.....' How can I fix this problem?
Share Improve this question edited Jul 23, 2015 at 10:36 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Jun 21, 2011 at 19:26 Anand S KumarAnand S Kumar 91.1k18 gold badges194 silver badges178 bronze badges 6-
Formatting your code may help people read it. Also, have you tried debugging to see if
mainj
andmainj.style
are undefined? – James Montagne Commented Jun 21, 2011 at 19:27 - formatting? i didnt understand – Anand S Kumar Commented Jun 21, 2011 at 19:29
- You have what should be 10-15 lines of code jammed into 6 with no indenting or spacing. It's hard to read that way. – James Montagne Commented Jun 21, 2011 at 19:31
- @Anand Split every instruction to one line so we can see which instruction causes the error exactly – Pekka Commented Jun 21, 2011 at 19:31
-
i got that, and yea
mainj
seems to be undefined but it shouldn't be so, because a element with id 'main1' exists and this code works perfectly fine without errors in firefox. – Anand S Kumar Commented Jun 21, 2011 at 19:32
2 Answers
Reset to default 8Make sure you have a DOM element with ID=main1
If it does not exist then your code will return errors.
what you can do is have a conditional:
if(mainj !== undefined) { //if mainj is a real object
//do code with mainj
}
Also try using local variables in your code (precede variables with the word var
) so they do not enter the global scope.
This is the offending line:
size=window.innerWidth-300;
Internet Explorer doesn't have a concept of inner width on window, as far as I know. The following code is a decent cross-browser implementation, similar to something you would see in a library. There are corresponding innerHeight and offsetHeight properties if you needed height as well.
var size = 0;
if(window.innerWidth) {
size = window.innerWidth;
} else if (document.documentElement && document.documentElement.offsetWidth) {
size = document.documentElement.offsetWidth;
} else if (document.body && document.body.offsetWidth) {
size = document.body.offsetWidth;
}
本文标签: JavaScript Error at documentgetElementById() in Internet ExplorerStack Overflow
版权声明:本文标题:JavaScript: Error at document.getElementById() in Internet Explorer - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744754723a2623392.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论