Javascript Error : Is Null or Not an Object
i am using html inputbox , which is inside update panel i need the cursor to be set focused on that inputbox once the page is loaded so i called a function as follows
< body onload="javascript:document.Default.UserInputTextBox.focus();" >
but i getting the following Javascript error,
Microsoft JScript runtime error: 'document.Default.UserInputTextBox' is null or not an object
please help on this开发者_如何学Go
You've got to wait for the DOM to be ready before trying to access elements in the dom; The onload event listener can wait for the entire document to be loaded including images and css... Which might be undesirable.
<head>
<script type="text/javascript" charset="utf-8">
// wait for the DOCUMENT to become ready.
window.onload=function(){
walkmydog()
}
</script>
</head>
Here's a detailed explanation for overcoming this sort of problem: http://www.javascriptkit.com/dhtmltutors/domready.shtml
A lot of people use javascript frameworks, like jQuery, MooTools, YUI, to overcome the browse incompatibility problems.
I do not think it is in the best interest of accessibility to dynamically alter a user's focus prior to alerting the user of such a change. What you can do that is accessible is redirect a user to a fragment URI pointing to the id attribute of the document object in question. That way you are not having to use any JavaScript.
So, for instance, if a user requests a page at this URI:
http://example.com/page.html
You can redirect the user with your ASP code and a HTTP 302 code to this page:
http://example.com/page.html#myInputIDValue
That would presume the input field in question had the following attribute:
id="myInputIDValue"
精彩评论