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

Cannot read property 'style' of null - in Chrome's Javascript Console - Stack Overflow

programmeradmin0浏览0评论

I was checking the JS console out and it helped me fix a few minor issues, but I would like to know if this warning is anything to seriously worry about?

This is the code which gives the error:

<script type="text/javascript"
src=".js"></script>
<script src=".js"             type="text/javascript"></script>
<script>
function layerSetup(id,visibility){
if(document.getElementById){
this.obj = document.getElementById(id).style;
Uncaught TypeError: Cannot read property 'style' of null
Uncaught TypeError: Cannot read property 'style' of null
<!--Includes many more of the same "Cannot read property 'style' of null messages -->

this.obj.visibility = visibility;
return this.obj;}
else if(document.all){
this.obj = document.all[id].style;
this.obj.visibility = visibility;
return this.obj;}
else if(document.layers){
this.obj = document.layers[id];
this.obj.visibility = visibility;
return this.obj;}
}
function visVisible(param){
new layerSetup(param,'visible');
}

function visHidden(param){
new layerSetup(param,'hidden');
}</script>

I cannot really figure out why this is happening and whether it's something I should worry about since our Rug categories function fine. Can anybody offer any insight? I apologize for not offering any of my own explanations but I didn't write this JS a former co-worked did and it's now up to me to debug every mistake he made. It's a learning process but I'm still somewhat new to this ..

I was checking the JS console out and it helped me fix a few minor issues, but I would like to know if this warning is anything to seriously worry about?

This is the code which gives the error:

<script type="text/javascript"
src="https://www.safaviehhome./product_images/locations/js/jqgalscroll.js"></script>
<script src="https://www.safaviehhome./product_images/mainnav/stuHover.js"             type="text/javascript"></script>
<script>
function layerSetup(id,visibility){
if(document.getElementById){
this.obj = document.getElementById(id).style;
Uncaught TypeError: Cannot read property 'style' of null
Uncaught TypeError: Cannot read property 'style' of null
<!--Includes many more of the same "Cannot read property 'style' of null messages -->

this.obj.visibility = visibility;
return this.obj;}
else if(document.all){
this.obj = document.all[id].style;
this.obj.visibility = visibility;
return this.obj;}
else if(document.layers){
this.obj = document.layers[id];
this.obj.visibility = visibility;
return this.obj;}
}
function visVisible(param){
new layerSetup(param,'visible');
}

function visHidden(param){
new layerSetup(param,'hidden');
}</script>

I cannot really figure out why this is happening and whether it's something I should worry about since our Rug categories function fine. Can anybody offer any insight? I apologize for not offering any of my own explanations but I didn't write this JS a former co-worked did and it's now up to me to debug every mistake he made. It's a learning process but I'm still somewhat new to this ..

Share Improve this question asked Jun 4, 2012 at 19:46 LivingstonLivingston 311 gold badge1 silver badge5 bronze badges 1
  • Without seeing your markup or how layerSetup is being called, I'd guess that you are trying to get an element by ID that does not exist in your document. – ajm Commented Jun 4, 2012 at 19:50
Add a ment  | 

2 Answers 2

Reset to default 2

This could be happening for two reasons.

  1. Somewhere on the page the layerSetup function is being called with null as one of the parameters.
  2. An element with the id being passed the the layerSetup function does not exist on the page.

In the Chrome dev console you can click Pause on All Exceptions, a button in the bottom left bar with a pause symbol. This will help you to determine what the parameters are that are being passed to the function.

Avoid document.all

http://www.javascripttoolbox./bestpractices/

http://simonwillison/2003/Aug/11/documentAll/

发布评论

评论列表(0)

  1. 暂无评论