show javascript info if no variable exsist

Posted on 2006-04-03
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")
end if

I am not really good with javascript so I could really use the help thanks ahead a time
Question by:BTMExpert
    LVL 20

    Accepted Solution

    would be something like this...

        document.write("variable doesn't exsist");
    LVL 9

    Expert Comment

    Maybe a nicer way :

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

    This check if variable is undefined.
    LVL 28

    Expert Comment

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

    <title>Catch error </title>

    <script language="javascript">
    function handleErr(err)
    txt="There was an error on this page.\n\n"
    txt+="Error: " + err + "\n"
    return true
    alert ('Message  ' + myundefinedvar);

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now