473,411 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

string value not being recognised as such


What I thought was a string value does not seem to be recognised as
such.
In the following code I extract the string "foo" from an array and
put it into the variable up0
But on checking I find that
(up0=="foo") returns false.

Using the toString function as in
var up0 = update[0].toString();
makes no difference

This is part of a bare bones AJAX demonstration known as
Rasmus' 30 second AJAX Tutorial

The string "foo|foo done" is returned by http.responseText
The aim is to use it to locate <div id="foo"and to display "foo
done" as its innerHTML
The code returns an errot at
document.getElementById(up0)

A lot of the following lines of code are variables and alerts I have
introduced to try to pinpoint where the problem lies.

Here is the code in more detail followed by the results reported in
the alerts
========================:

function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
var update = new Array();
if(response.indexOf('|' != -1)) {
update = response.split('|');
// the following lines are for debugging
// and not part of the original code
var lgth = update.length;
alert(" ALERT 0 - respose string is " + response);
alert(" ALERT 1 - update length is " + lgth);
var up0 = update[0];
var up1 = update[1].toString();
alert( " ALERT 2 - " + up0 + " and " + up1 ) ;
if(up0=="foo"){
alert(" ALERT 3 - up0 is foo");
}else{
alert(" ALERT 3 - up0 is NOT foo");
}
if(up1=="foo done"){
alert(" ALERT 4 - is done");
} else{
alert(" ALERT 4 - is NOT foo done");
}
document.getElementById("update0").innerHTML = update[0];
document.getElementById("update1").innerHTML = update[1];
// debugging code ends

document.getElementById(up0).innerHTML = update[1];
}
}
}

</script>

<a href="javascript:sndReq('foo')">click me</a>
<div id="update0" </div>
<div id="update1" </div>
<div id="foo" </div>
=========================================
ALERT 0 showed response as 'foo|foo done'
ALERT 1 showed update.length as 2
ALERT 2 showed up0 as 'foo' and up1 as 'foo done'
ALERT 3 reported "up0 is NOT 'foo' "
ALERT 4 reported "up1 is NOT 'foo done' "
<div id="update0" innerHTML displayed as "foo"
<div id="update1" innerHTML displayed as "foo done"

And here is the real problem :
document.getElementById(up0).innerHTML = update[1];
triggered an error message saying
document.getElementById(...)is either null or not an object
I would be grateful if someone could explain what is happening here.
Why does variable up0 display like it is a string but when I try to
use it as a value it is not recognised?

TIA
N



TIA
N
Mar 21 '07 #1
5 1507
noddy <no***@toyland.comwrote:
I would be grateful if someone could explain what is happening here.
Why does variable up0 display like it is a string but when I try to
use it as a value it is not recognised?
I would guess you probably have some whitespace in the response text. Try
putting quotes round the strings in the alerts, e.g.

alert(" ALERT 2 - '" + up0 + "' and '" + up1 + "'") ;

Or, better, use a debugger and set a breakpoint so you can view the actual
values of the variables. Both Firefox and IE have excellent debuggers
available so there really is now reason to be using alerts to see variable
values. Try Firebug for Firefox or any of Microsoft's assorted debuggers
for IE (currently when I have to debug IE I use the one included with
Microsoft Office 2000).
Mar 21 '07 #2
noddy wrote:
>
What I thought was a string value does not seem to be recognised as
such.
In the following code I extract the string "foo" from an array and
put it into the variable up0
But on checking I find that
(up0=="foo") returns false.

Using the toString function as in
var up0 = update[0].toString();
makes no difference

This is part of a bare bones AJAX demonstration known as
Rasmus' 30 second AJAX Tutorial

The string "foo|foo done" is returned by http.responseText
The aim is to use it to locate <div id="foo"and to display "foo
done" as its innerHTML
The code returns an errot at
document.getElementById(up0)

A lot of the following lines of code are variables and alerts I have
introduced to try to pinpoint where the problem lies.

Here is the code in more detail followed by the results reported in
the alerts
========================:

function handleResponse() {
if(http.readyState == 4){
Hi,

Maybe your response contains more characters than you expect.
I would start by replacing the next line to:
var response = http.responseText;
to

var response = 'foo|foo done';
var update = new Array();
if(response.indexOf('|' != -1)) {
Tom said already the above line is wrong.
Do you want to have the indexOf
'|'
or do you want to have the indexOf of whatever
'|' != -1
produces?
I think '|' != -1 always evalutes to 'true'.

so you are effective looking for indexOf('true') which returns -1, which
evaluates in your if() to true.
Here follows a slightly modified version of your script that behaves as
expected:

<html>
<body>

<div id="update0" </div>
<div id="update1" </div>

<script type="text/javascript">

var response = 'foo|foo done';
var update = new Array();
if(response.indexOf('|') != -1) {
update = response.split('|');
// the following lines are for debugging
// and not part of the original code
var lgth = update.length;
alert(" ALERT 0 - respose string is " + response);
alert(" ALERT 1 - update length is " + lgth);
var up0 = update[0];
var up1 = update[1].toString();
alert( " ALERT 2 - " + up0 + " and " + up1 ) ;
if(up0=="foo"){
alert(" ALERT 3 - up0 is foo");
}else{
alert(" ALERT 3 - up0 is NOT foo");
}

if(up1=="foo done"){
alert(" ALERT 4 - is done");
} else{
alert(" ALERT 4 - is NOT foo done");
}
document.getElementById("update0").innerHTML = "update[0] = "+update[0];
document.getElementById("update1").innerHTML = "update[1] = "+update[1];
// debugging code ends

}

</script>
</body>
</html>

Regards,
Erwin Moller
Mar 21 '07 #3
On 21 Mar 2007 09:53:07 GMT, Duncan Booth
<du**********@invalid.invalidwrote:
>Or, better, use a debugger and set a breakpoint so you can view the actual
values of the variables. Both Firefox and IE have excellent debuggers
available so there really is now reason to be using alerts to see variable
values. Try Firebug for Firefox or any of Microsoft's assorted debuggers
for IE (currently when I have to debug IE I use the one included with
Microsoft Office 2000).
Many thanks for this advice Duncan.
I have just started a tutorial on how to
use Firebug :)

N
Mar 22 '07 #4
On Wed, 21 Mar 2007 11:02:20 +0100, Erwin Moller
<si******************************************@spam yourself.comwrote:

>Hi,
>Tom said already the above line is wrong.
Do you want to have the indexOf
'|'
or do you want to have the indexOf of whatever
'|' != -1
produces?
I think '|' != -1 always evalutes to 'true'.

so you are effective looking for indexOf('true') which returns -1, which
evaluates in your if() to true.
Thank you for your patience in explaining this to me Erwin.
It has been a great help

N
Mar 22 '07 #5
On 21 Mar 2007 09:53:07 GMT, Duncan Booth
<du**********@invalid.invalidwrote:
>noddy <no***@toyland.comwrote:
>I would be grateful if someone could explain what is happening here.
Why does variable up0 display like it is a string but when I try to
use it as a value it is not recognised?
Using Firebug I discovered that the string had
/n characters at either end.
removing them has solved the problem.

Thanks again
N

Mar 23 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Garfield | last post by:
Hello I have a function that returns a string array. The string has a name and an ID number, they are separated by a comma. I cannot for the life of me by using the string methods get to...
50
by: Doug | last post by:
I can't imagine this is that hard but I'm sure having a struggle finding it. How do I convert a value like "111403" do a DateTime variable in DotNet (i.e. 11/14/2003)?
2
by: Kevin Blount | last post by:
I have a string of HTML (used for a specific purpose) that I'd like to use somewhere else but as plain text. Rather than introduce a specifically created plain text version I'd like to strip the...
7
by: 4004 | last post by:
Where am I going wrong? I have a form which has a record for each date on which I teach. It has a subform which shows the classes that I teach that day. So I want to be able to click on a...
6
by: Jase | last post by:
Hi, I have two servers, both running windows 2003 and both containing the exact same properties for IIS. I have a set of asp intranet pages that are located inside the c:\inetpub\wwwroot\test...
31
by: Peter Michaux | last post by:
Hi, I want to know the name of an object's constructor function as a string. Something like this <script type="text/javascript"> function Foo(){}; var a = new Foo(); alert('"' +...
17
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I get the value of a form control? -----------------------------------------------------------------------...
1
by: pal.saurabh | last post by:
I've an XML file as follows. <?xml version="1.0"?> <packet> <proto name="bootp" size="272" pos="46"> <field name="bootp.type" size="1" pos="46" show="1" value="01"/></ field> <field...
7
by: Andrew Poulos | last post by:
I'm creating a JSON string and passing it to a SCORM 1.2 compliant LMS thus: var tmp = {}; tmp.assess = assessScore; tmp.loc = location; tmp.progress = notes.topics.toString();...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.