The key is the if statement. getElementById will return nothing null if the element is not found, which will evaluate to false in the if statement.

Alternatively, if you don't really want to check for existence of individual DOM elements, can you send the users priv in a hidden input and act on that? That's a cookie free way of sending values clientside. Something like (edited to have jQuery code instead)