We help IT Professionals succeed at work.

show javascript info if no variable exsist

BTMExpert
BTMExpert asked
on
Medium Priority
208 Views
Last Modified: 2012-08-14
I have a page on my website and I want it to  print a string if a variable doesn't exsist if the variable does exsist then I want it to print the variable instead something like

if showstring = null then
document.write("variable doesn't exsist")
else
document.write(showstring)
end if

I am not really good with javascript so I could really use the help thanks ahead a time
Comment
Watch Question

would be something like this...

<script>
  if(showstring==""||showstring=="null"){
    document.write("variable doesn't exsist");
  }else{
    document.write(showstring);
  }
</script>

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
Maybe a nicer way :

if (typeof(showstring)=="undefined") document.write("variable doesn't exsist");
else document.write(showstring);

This check if variable is undefined.
Pravin AsarPrincipal Systems Engineer
CERTIFIED EXPERT
Top Expert 2005

Commented:
Here is very generic way to handle Error, by assigning onError event handler for a document.

<html>
<head>
<title>Catch error </title>
</head>

<body>
<script language="javascript">
function handleErr(err)
{
txt="There was an error on this page.\n\n"
txt+="Error: " + err + "\n"
alert(txt)
return true
}
onerror=handleErr
alert ('Message  ' + myundefinedvar);
</script>
</body>
</html>
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.