admin管理员组文章数量:1331849
I am getting this error when I call a javascript function to display a modal window:
Microsoft JScript runtime error: 'document.getElementById(...)' is null or not an object
The code block is:
else if (action=="officeview") {
document.getElementById("OfficeContent").src="ChangeView.aspx";
ShowFeatureModal('AppView','OfficeContent')
The object is this situation, does exist.
Error is caused at: document.getElementById
line.
What else could be causing the error?
Update:
Index.aspx is calling the javascript function which is located in sysUtilities.js file. The source file is yet a seperate page (ChangeView.aspx)
I am getting this error when I call a javascript function to display a modal window:
Microsoft JScript runtime error: 'document.getElementById(...)' is null or not an object
The code block is:
else if (action=="officeview") {
document.getElementById("OfficeContent").src="ChangeView.aspx";
ShowFeatureModal('AppView','OfficeContent')
The object is this situation, does exist.
Error is caused at: document.getElementById
line.
What else could be causing the error?
Update:
Index.aspx is calling the javascript function which is located in sysUtilities.js file. The source file is yet a seperate page (ChangeView.aspx)
-
1
document.getElementById
returnsnull
if the element is not found, and accessing.src
ofnull
is not allowed. Where are you using this code? – Digital Plane Commented Aug 17, 2011 at 15:39 -
If you look at the source code of your page (in the browser), can you find a HTML element with
id="OfficeContent"
? – M4N Commented Aug 17, 2011 at 15:42 - @DigitalPlane not sure what you mean by "where am I using this code" It is being used client side, to call a modal window. The user clicks a hyperlink, and this code block is called. – DNR Commented Aug 17, 2011 at 15:43
- @M4N... the page ChangeView.aspx does not load.... when I click on the hyperlink, nothing happens (except for the javascript crash dialog box) – DNR Commented Aug 17, 2011 at 17:17
4 Answers
Reset to default 3If document.getElementById
doesn't find the element, it will return null
. If you then try to get the src
property from null
, you'll get this error.
You'll either need to ensure the there's an element with its ID equal to OfficeContent
or do something like the following:
else if (action=="officeview") {
var officeContent = document.getElementById("OfficeContent")
if (officeContent) {
officeContent.src="ChangeView.aspx";
ShowFeatureModal('AppView','OfficeContent')
}
}
EDIT: If you're using ASP.NET, which it appears that you are, remember that your IDs might be getting name-mangled if they are inside a container control. In that case, you have to make sure to use the ClientID
, not the plain old ID
. Something like this:
document.getElementById("<%= OfficeContent.ClientID %>")
Don't know if it will help in this case, but this is a trick to prevent the error:
(document.getElementById("OfficeContent")||{}).src="ChangeView.aspx";
If the element doesn't exist, an empty object gets the src
-property, no error is thrown, no harm is done.
It may be wise though to look for the cause of document.getElementById("OfficeContent")
returning null.
you need test whether an element exists first before setting element's src attribute
var el = document.getElementById("OfficeContent");
el && (el.src="ChangeView.aspx");
All this stuff is peripheral to the main issue, which is:
You must use the actual clientID of "OfficeContent", which may be quite different in the HTML DOM of the page as it is rendered. One easy way to avoid this would look something like this:
var officeContent = document.getElementById("<%=OfficeContent.ClientID %>")
本文标签:
版权声明:本文标题:asp.net - javascript error: Microsoft JScript runtime error: 'document.getElementById(...)' is null or not an ob 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742234188a2437769.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论