 |
| 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
|
|
|
|

September 29th, 2007, 03:46 PM
|
|
Authorized User
|
|
Join Date: Sep 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|
|

September 29th, 2007, 04:23 PM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
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
================================================== =========
|
|

September 29th, 2007, 06:31 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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)
|
|

September 30th, 2007, 12:57 AM
|
|
Authorized User
|
|
Join Date: Sep 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

September 30th, 2007, 07:02 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Show your exact code again, last time it checked for a string of one space, not an empty one.
--
Joe ( Microsoft MVP - XML)
|
|

September 30th, 2007, 07:12 AM
|
|
Authorized User
|
|
Join Date: Sep 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

September 30th, 2007, 07:18 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Okay, can you add
Code:
alert("!" + askedUser + "!");
just before the "if" line?
--
Joe ( Microsoft MVP - XML)
|
|

September 30th, 2007, 07:28 AM
|
|
Authorized User
|
|
Join Date: Sep 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I did, it seems it includes spaces, the alert box displays:
|
|

September 30th, 2007, 07:50 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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)
|
|

September 30th, 2007, 10:08 AM
|
|
Authorized User
|
|
Join Date: Sep 2007
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you Joe!
This is the second time you're resolving my problem, I really appreciate that
|
|
 |