admin管理员组

文章数量:1330565

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)

Share Improve this question edited Aug 17, 2011 at 20:48 DNR asked Aug 17, 2011 at 15:36 DNRDNR 3,74614 gold badges57 silver badges95 bronze badges 4
  • 1 document.getElementById returns null if the element is not found, and accessing .src of null 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
Add a ment  | 

4 Answers 4

Reset to default 3

If 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 %>")

本文标签: