Problems with a value being NULL

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Problems with a value being NULL

Post by Wolf_22 »

I have a PHP script that determines whether or not a login form is generated for the end-user. If user is logged in, then no login prompt. Easy-peasy. Outside of the PHP conditioning, though, I have a JavaScript file that is referenced at all times. In this file, I have a function that simply places the text focus on the login prompt at the page load. Since the login prompt is generated conditionally (whether the user is logged in or not), I'm getting NULL errors at every page load where there is no login prompt due to the fact that the function is referenced as an onload event.

Besides going through the hassle of making a completely new JavaScript file and then in turn placing that JavaScript focus function in it and placing the new JavaScript file reference inside of some more PHP conditions, is there a simpler way to fix this NULL issue? I was really wanting to keep all my JavaScript in 1 single file for this, but since the browser is referencing that file at all times, it's causing some issues and making me start to believe I need another file for when someone is not logged in--which seems like unnecessary overhead.

I was thinking that maybe I could do something like "if object not null, then define function", but I'm not sure how to do this in JavaScript. The thing this is happening to is "document.getElementByID('user_login').focus". How would I make the JavaScript check to see if that value (or element?) is NULL?
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: Problems with a value being NULL

Post by Wolf_22 »

Man, I'm sorry. I think I fixed it...

I placed the following inside the function definition:

Code: Select all

if(document.getElementById('user_login') !== null)
Still though, if anyone has a better approach, I'm all ears (eyes). :)
Post Reply