[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

show javascript info if no variable exsist

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
0
BTMExpert
Asked:
BTMExpert
1 Solution
 
trailblazzyr55Commented:
would be something like this...

<script>
  if(showstring==""||showstring=="null"){
    document.write("variable doesn't exsist");
  }else{
    document.write(showstring);
  }
</script>
0
 
smaccariCommented:
Maybe a nicer way :

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

This check if variable is undefined.
0
 
Pravin AsarCommented:
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>
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now