By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,481 Members | 1,211 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,481 IT Pros & Developers. It's quick & easy.

Why won't this js work properly?

P: n/a
In the following, my first attempt at using hash array, if 'score' == 0 or
'undefined' the following script fails with the js-console message:
'Could not convert undefined or null to object
document.getElementById(target1).innerHTML = regOut;
decodeRegion(0, "target")'

var region = new Array();
region["0"]="an unspecified region";
region["undefined"]="an unspecified region";
region["11"]="the upper-outer quadrant";
etc.

function decodeRegion(score,target) {
if (score == 0 || score == 'undefined') {return;} // Must have this**
if (score > 100) {regOut1 = ' and other'; score -= 100;}
regOut = region[score] ;
document.getElementById(target).innerHTML = regOut;
return;
}

** If I don't escape, I get the error.

I've tried things like if(score == 0) {regOut = 'somestring;} after
regOut=region[score] , and also guaranteeing that score is a string using
score.toString(10), but it still doesn't play.

--
Ed Jay (remove M to respond by email)
Dec 27 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a

Ed Jay wrote:
function decodeRegion(score,target) {
if (score == 0 || score == 'undefined') {return;} // Must have this**
First off you're comparing to see if score is equal to a _string_
'undefined'. What you probably meant was to use the keyword undefined
instead.

score == undefined
document.getElementById(target).innerHTML = regOut; From the error message you received, I would say your problem lies right on this line. And also judging from your error message, you have
not thought to check to see if target is null or undefined. Also it
would be nice to check for object functionality:

if(document.getElementById)
{
if(target)
{
document.getElementById(target).innerHTML = regOut;
}
}
return;


This is just a personal preference, but in this case I don't see a need
to return undefined. Does your function really need to return
undefined? Or once you return undefined, will you be using that value
somewhere?

Dec 27 '05 #2

P: n/a
"web.dev" <we********@gmail.com> wrote:

Ed Jay wrote:
function decodeRegion(score,target) {
if (score == 0 || score == 'undefined') {return;} // Must have this**
First off you're comparing to see if score is equal to a _string_
'undefined'. What you probably meant was to use the keyword undefined
instead.

score == undefined


OK.
document.getElementById(target).innerHTML = regOut;
From the error message you received, I would say your problem lies

right on this line. And also judging from your error message, you have
not thought to check to see if target is null or undefined. Also it
would be nice to check for object functionality:


I'm using <span id="target"></span> for all my placeholders. I've checked
(carefully) and they're all there.

As an aside...is there a limit to 'target.length?'

Also, I'm calling the function ten times with different scores and targets
using <body onLoad="....>. Is it possible there's a timing issue?
if(document.getElementById)
{
if(target)
{
document.getElementById(target).innerHTML = regOut;
}
}
return;


This is just a personal preference, but in this case I don't see a need
to return undefined. Does your function really need to return
undefined? Or once you return undefined, will you be using that value
somewhere?


I automatically use it as an escape. It's no big deal.

--
Ed Jay (remove M to respond by email)
Dec 27 '05 #3

P: n/a
web.dev wrote:
Ed Jay wrote:
function decodeRegion(score,target) {
if (score == 0 || score == 'undefined') {return;} // Must have this**
First off you're comparing to see if score is equal to a _string_
'undefined'. What you probably meant was to use the keyword undefined
instead.

score == undefined


No, it is far more reliable to compare

typeof score == "undefined"

And it is more efficient to test

if (typeof score == 'undefined' || score == 0)
{
return;
}

because if the type is undefined, it is not necessary
to check for the value. Even more efficient would be

if (!score)
{
return;
}

but that would apply to any false-value.
document.getElementById(target).innerHTML = regOut;

From the error message you received, I would say your problem lies

right on this line. And also judging from your error message, you have


Please learn how to post. Quoted text should be clearly distinguishable
to your text.
not thought to check to see if target is null or undefined. Also it
would be nice to check for object functionality:

if(document.getElementById)
That is testing for value, not functionality.

<URL:http://pointedears.de/scripts/test/whatami#inference>
{
if(target)
{
document.getElementById(target).innerHTML = regOut;


And that is error-prone nonsense.

The _minimum_ required for this not to break with certain values is

function isMethodType(s)
{
return (s == "function" || s == "object");
}

if (isMethodType(typeof document.getElementById))
{
var o;
if ((o = document.getElementById(target)))
{
o.innerHTML = regOut;
}
}
PointedEars
Dec 28 '05 #4

P: n/a
Thomas 'PointedEars' Lahn <Po*********@web.de> wrote:
web.dev wrote:
Ed Jay wrote:
function decodeRegion(score,target) {
if (score == 0 || score == 'undefined') {return;} // Must have this**
First off you're comparing to see if score is equal to a _string_
'undefined'. What you probably meant was to use the keyword undefined
instead.

score == undefined


No, it is far more reliable to compare

typeof score == "undefined"

And it is more efficient to test

if (typeof score == 'undefined' || score == 0)
{
return;
}

because if the type is undefined, it is not necessary
to check for the value. Even more efficient would be

if (!score)
{
return;
}

but that would apply to any false-value.
document.getElementById(target).innerHTML = regOut;

From the error message you received, I would say your problem lies

right on this line. And also judging from your error message, you have


Please learn how to post. Quoted text should be clearly distinguishable
to your text.
not thought to check to see if target is null or undefined. Also it
would be nice to check for object functionality:

if(document.getElementById)


That is testing for value, not functionality.

<URL:http://pointedears.de/scripts/test/whatami#inference>
{
if(target)
{
document.getElementById(target).innerHTML = regOut;


And that is error-prone nonsense.


Why is it error prone?

Taking your lead from above, how about:

if (!score || !target)
{
return;
}

which appears to work without error.
The _minimum_ required for this not to break with certain values is

function isMethodType(s)
{
return (s == "function" || s == "object");
}

if (isMethodType(typeof document.getElementById))
{
var o;
if ((o = document.getElementById(target)))
{
o.innerHTML = regOut;
}
}

Thank you for your comments.

--
Ed Jay (remove M to respond by email)
Dec 28 '05 #5

P: n/a
Thomas 'PointedEars' Lahn said the following on 12/27/2005 7:09 PM:

<snip>

The _minimum_ required for this not to break with certain values is
No, it is not the "minimum required for this not to break".
function isMethodType(s)
{
return (s == "function" || s == "object");
}

if (isMethodType(typeof document.getElementById))
{
var o;
if ((o = document.getElementById(target)))
{
o.innerHTML = regOut;


And if the browser doesn't support innerHTML? You use the feature but do
not test for it.

The only reliable way to test for innerHTML support, to date, is to
insert non-normalized HTML into a container using innerHTML and then
read it back. If it is normalized, the browser supports innerHTML. If it
is not normalized, then the browser doesn't support innerHTML.

And when I say "supports innerHTML", I am not referring to simply
passing a test for it. I am referring to actually changing the inner
html of an object when scripting it's innerHTML property.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 28 '05 #6

P: n/a
On 2005-12-27, Ed Jay <ed***@aes-intl.com> wrote:
In the following, my first attempt at using hash array, if 'score' == 0
javascript does not have "hash" or "associative" arrays, you code as adding
properties to an object or elements to an array depending on the index value
you use..
or
'undefined' the following script fails with the js-console message:
'Could not convert undefined or null to object
document.getElementById(target1).innerHTML = regOut; decodeRegion(0, "target")'

var region = new Array();
region[0]="OOPS!"; // see if that makes any difference.
region["0"]="an unspecified region";
region["undefined"]="an unspecified region";
region["11"]="the upper-outer quadrant";
etc.



--

Bye.
Jasen
Dec 28 '05 #7

P: n/a
Jasen Betts <ja***@free.net.nospam.nz> wrote:
On 2005-12-27, Ed Jay <ed***@aes-intl.com> wrote:
In the following, my first attempt at using hash array, if 'score' == 0
javascript does not have "hash" or "associative" arrays, you code as adding
properties to an object or elements to an array depending on the index value
you use..


A rose, by any other name... :-) (My books use the term hash array and
associative array interchangeably.)
or
'undefined' the following script fails with the js-console message:
'Could not convert undefined or null to object
document.getElementById(target1).innerHTML = regOut;
decodeRegion(0, "target")'

var region = new Array();


region[0]="OOPS!"; // see if that makes any difference.


Sure does...stopped it from working.
region["0"]="an unspecified region";
region["undefined"]="an unspecified region";
region["11"]="the upper-outer quadrant";
etc.


--
Ed Jay (remove M to respond by email)
Dec 28 '05 #8

P: n/a
Randy Webb wrote on 28 dec 2005 in comp.lang.javascript:
And when I say "supports innerHTML", I am not referring to simply
passing a test for it. I am referring to actually changing the inner
html of an object when scripting it's innerHTML property.


Like this?

<div id=d>Hello world</div>

<script type='text/javascript'>

var d = document.getElementById('d')
d.innerHTML='<br>'
if (d.innerHTML==='<BR>') // test for uppercase!
alert('normalized innerHTML')

</script>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 28 '05 #9

P: n/a
Evertjan. said the following on 12/28/2005 5:02 AM:
Randy Webb wrote on 28 dec 2005 in comp.lang.javascript:

And when I say "supports innerHTML", I am not referring to simply
passing a test for it. I am referring to actually changing the inner
html of an object when scripting it's innerHTML property.
Like this?

<div id=d>Hello world</div>

<script type='text/javascript'>

var d = document.getElementById('d')
d.innerHTML='<br>'


I think it used something more along the lines of:

newHTML = '<s Pa N>SomeText</ s PA n>';

And then checked to see if the new innerHTML.toLowerCase() was equal to
newHTML.toLowerCase() to see whether it normalized the spaces out of the
tags.
if (d.innerHTML==='<BR>') // test for uppercase!
alert('normalized innerHTML')


Yes, that is the basic idea :)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 28 '05 #10

P: n/a
JRS: In article <_L********************@comcast.com>, dated Tue, 27 Dec
2005 21:31:26 local, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :

The only reliable way to test for innerHTML support, to date, is to
insert non-normalized HTML into a container using innerHTML and then
read it back. If it is normalized, the browser supports innerHTML. If it
is not normalized, then the browser doesn't support innerHTML.

And when I say "supports innerHTML", I am not referring to simply
passing a test for it. I am referring to actually changing the inner
html of an object when scripting it's innerHTML property.

(1) Is it possible for a browser to support innerHTML as seen from the
scripting side, and yet not to make the corresponding effect on its
visible screen?

(2) Could you get your grammar checker fixed? ISTM that it's broken.
One should mot lead astray those who have actually learned proper
English.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Dec 29 '05 #11

P: n/a
Dr John Stockton said the following on 12/29/2005 11:44 AM:
JRS: In article <_L********************@comcast.com>, dated Tue, 27 Dec
2005 21:31:26 local, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
The only reliable way to test for innerHTML support, to date, is to
insert non-normalized HTML into a container using innerHTML and then
read it back. If it is normalized, the browser supports innerHTML. If it
is not normalized, then the browser doesn't support innerHTML.

And when I say "supports innerHTML", I am not referring to simply
passing a test for it. I am referring to actually changing the inner
html of an object when scripting it's innerHTML property.
(1) Is it possible for a browser to support innerHTML as seen from the
scripting side, and yet not to make the corresponding effect on its
visible screen?


Anything is possible so yes it's possible. Is there any way to test for
it other than physically looking at the monitor to see if it changed the
text? No.
(2) Could you get your grammar checker fixed?
There is nothing wrong with my grammar checker.
ISTM that it's broken.
Then you seem to think incorrectly.
One should mot lead astray those who have actually learned proper
English.


Two things:

1)If one is taught "proper English" and gets lead astray by anything I
write then they were not taught properly.

2)Before you get pedantic about grammar and/or spelling perhaps you
should get *your* checkers checked.

mot != not

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

Dec 29 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.