Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 29th, 2007, 03:46 PM
Authorized User
 
Join Date: Sep 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to know if a variable is null

Hi,

I try to check if a variable is null via following code but I don't get an expected result:
Code:
 var askedUser = re.responseText;
            askedUser = askedUser.substr(0, askedUser.indexOf('&',0));
            if(askedUser != null  && askedUser != " ")
            {    do something}


the if() returns true whether askedUser == "" or askedUser = "a value"!
Could anyone guide me please?


 
Old September 29th, 2007, 04:23 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

My previous statement was inaccurate, sorry about that, it has been a long day. What happens if you phsyically set the value?

var askedUser = 'Some Value'

Do you drop into the if or no?

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========
 
Old September 29th, 2007, 06:31 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

It's not clear whether peace2007 wants to test for an empty string or a single space. Perhaps the test should be:
Code:
if (askedUser)
{
  //this will run if user is not null and not an empty string.
}
--

Joe (Microsoft MVP - XML)
 
Old September 30th, 2007, 12:57 AM
Authorized User
 
Join Date: Sep 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm checking for an empty string; either I physically set the value or modify the condition as "if (askedUser)" I don't get the desired result!
The whole process is as follows:
I get a value from server, then remove the extra characters from the end of it, and then I check if the value is an empty string (I set the return result of server to string.empty by default)
While I'm tracing my code when I reach to the statement before if condition, the value of askedUser is (askedUser = "") but the if() returns true!

Thanks for your replies
 
Old September 30th, 2007, 07:02 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Show your exact code again, last time it checked for a string of one space, not an empty one.

--

Joe (Microsoft MVP - XML)
 
Old September 30th, 2007, 07:12 AM
Authorized User
 
Join Date: Sep 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is the exact copy of my code:
Code:
url = 'Server.aspx?action=GetVideoReq&session=' + rnd ;
    var re = getAjax();

    re.onreadystatechange = function(){    
        if( re.readyState == 4 && re.status == 200 ) 
        {    
            var askedUser = re.responseText;
            askedUser = askedUser.substr(0, askedUser.indexOf('&',0));  
            if(askedUser != null  && askedUser != "")
            {    
                ...}


In which when askedUser == "" the if() returns true. I put nothing between two quotation marks.

 
Old September 30th, 2007, 07:18 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Okay, can you add
Code:
alert("!" + askedUser + "!");
just before the "if" line?

--

Joe (Microsoft MVP - XML)
 
Old September 30th, 2007, 07:28 AM
Authorized User
 
Join Date: Sep 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I did, it seems it includes spaces, the alert box displays:

Code:
!

!


 
Old September 30th, 2007, 07:50 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

So you've got some newline characters in there. Looks like you might have to trim the data before testing or find out why your web service is sending back that combination.

--

Joe (Microsoft MVP - XML)
 
Old September 30th, 2007, 10:08 AM
Authorized User
 
Join Date: Sep 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Joe!
This is the second time you're resolving my problem, I really appreciate that






Similar Threads
Thread Thread Starter Forum Replies Last Post
ASSIGNING A JAVA SCRIPT VARIABLE TO A XSL VARIABLE SOMANATHAN10 XSLT 1 February 21st, 2007 04:26 AM
Easy Question: Setting a variable to null Enoch Access 8 May 22nd, 2006 10:30 AM
How to set Not Null constraint to Null Columns arasu Oracle 1 August 22nd, 2005 10:09 AM
Object variable or With block variable not set tparrish VS.NET 2002/2003 3 May 22nd, 2005 07:40 AM
Error: (variable) is null or not an object alisonchase JSP Basics 1 February 14th, 2005 07:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.