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

using a minus in javascript

P: n/a
I have the following link to bring up an alert with a figure in it based on
the screen resolution. but instead of displaying a number 'NaN' is
displayed.

<a href="javascript:alert('Your ideal estimated height is '+
screen.height*0.55-screen.height*0.2);" class="BodyLink">Click to calculate
height </a>

But when I remove the part of the equation '-screen.height*0.2' a figure is
displayed properly. Why doesnt it work when I have a minus in the equation.
Im not experienced with javascript so excuse if this is a basic question

thanks
ian

Dec 3 '06 #1
Share this Question
Share on Google+
19 Replies


P: n/a
any good links to doing calculations with javascript would also be very much
appreciated
"mantrid" <ia********@virgin.netwrote in message
news:Wa*****************@newsfe2-gui.ntli.net...
I have the following link to bring up an alert with a figure in it based
on
the screen resolution. but instead of displaying a number 'NaN' is
displayed.

<a href="javascript:alert('Your ideal estimated height is '+
screen.height*0.55-screen.height*0.2);" class="BodyLink">Click to
calculate
height </a>

But when I remove the part of the equation '-screen.height*0.2' a figure
is
displayed properly. Why doesnt it work when I have a minus in the
equation.
Im not experienced with javascript so excuse if this is a basic question

thanks
ian

Dec 3 '06 #2

P: n/a
Lee
mantrid said:
>
I have the following link to bring up an alert with a figure in it based on
the screen resolution. but instead of displaying a number 'NaN' is
displayed.

<a href="javascript:alert('Your ideal estimated height is '+
screen.height*0.55-screen.height*0.2);" class="BodyLink">Click to calculate
height </a>

But when I remove the part of the equation '-screen.height*0.2' a figure is
displayed properly. Why doesnt it work when I have a minus in the equation.
Im not experienced with javascript so excuse if this is a basic question
You're actually having trouble with the "+" operator, not the "-".
Your code:
1. calculates screen.height*0.55
2. concatenates that value with "Your idea estimated height is ".
3. calculates screen.height*0.2
4. tries to subtract that value from the string result of step 2
which is Not a Number.

Enclose your subtraction in parentheses, so it will be performed
before the concatenation:

'Your ideal estimated height is '+(screen.height*0.55-screen.height*0.2)

You really shouldn't be abusing he side-effect of the javascript:
pseudo-protocol that way, but if you must, you should have the
event handler return false to prevent the page from reloading
each time the link is clicked:

href="javascript:alert('Your ideal estimated height is '+
(screen.height*0.55-screen.height*0.2));return false"
--

Dec 3 '06 #3

P: n/a
Your statement is being confused by something called operator
presidence. Try wrapping the math part of the equation in an extra set
of parens like this:

alert('Your ideal estimated height is ' + (screen.height * 0.55 -
screen.height * 0.2))
It forces the equation to evaluate a number before trying to convert it
to a string and attach it to your message. As you had it written, the
equation was doing the multiplication operators first, then adding the
"Your ideal...." string to the first number, making it a string. When
it tried to subtract the second number, the parser thought "I can't
subtract a number from a string" and threw NaN at you instead.

Cheers!

</bd>
mantrid wrote:
any good links to doing calculations with javascript would also be very much
appreciated
"mantrid" <ia********@virgin.netwrote in message
news:Wa*****************@newsfe2-gui.ntli.net...
I have the following link to bring up an alert with a figure in it based
on
the screen resolution. but instead of displaying a number 'NaN' is
displayed.

<a href="javascript:alert('Your ideal estimated height is '+
screen.height*0.55-screen.height*0.2);" class="BodyLink">Click to
calculate
height </a>

But when I remove the part of the equation '-screen.height*0.2' a figure
is
displayed properly. Why doesnt it work when I have a minus in the
equation.
Im not experienced with javascript so excuse if this is a basic question

thanks
ian

Dec 3 '06 #4

P: n/a
Thanks
You really shouldn't be abusing he side-effect of the javascript:
pseudo-protocol that way, but if you must, you should have the
event handler return false to prevent the page from reloading
each time the link is clicked:
excuse my ignorance but can you clarify this. why is it abuse?
Dec 3 '06 #5

P: n/a
thanks
that worked

"manxomfoe" <br**********@gmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
Your statement is being confused by something called operator
presidence. Try wrapping the math part of the equation in an extra set
of parens like this:

alert('Your ideal estimated height is ' + (screen.height * 0.55 -
screen.height * 0.2))
It forces the equation to evaluate a number before trying to convert it
to a string and attach it to your message. As you had it written, the
equation was doing the multiplication operators first, then adding the
"Your ideal...." string to the first number, making it a string. When
it tried to subtract the second number, the parser thought "I can't
subtract a number from a string" and threw NaN at you instead.

Cheers!

</bd>
mantrid wrote:
any good links to doing calculations with javascript would also be very
much
appreciated
"mantrid" <ia********@virgin.netwrote in message
news:Wa*****************@newsfe2-gui.ntli.net...
I have the following link to bring up an alert with a figure in it
based
on
the screen resolution. but instead of displaying a number 'NaN' is
displayed.
>
<a href="javascript:alert('Your ideal estimated height is '+
screen.height*0.55-screen.height*0.2);" class="BodyLink">Click to
calculate
height </a>
>
But when I remove the part of the equation '-screen.height*0.2' a
figure
is
displayed properly. Why doesnt it work when I have a minus in the
equation.
Im not experienced with javascript so excuse if this is a basic
question
>
thanks
ian
>
>
>

Dec 3 '06 #6

P: n/a
mantrid wrote:
any good links to doing calculations with javascript would also be very much
appreciated
"mantrid" <ia********@virgin.netwrote in message
news:Wa*****************@newsfe2-gui.ntli.net...
>>I have the following link to bring up an alert with a figure in it based

on
>>the screen resolution. but instead of displaying a number 'NaN' is
displayed.

<a href="javascript:alert('Your ideal estimated height is '+
screen.height*0.55-screen.height*0.2);" class="BodyLink">Click to

calculate
>>height </a>
Why not:
alert('Your ideal estimated height is '+screen.height*0.53)
?
screen.height*0.55-screen.height*0.2==screen.height*0.53, no?
Mick

>>
But when I remove the part of the equation '-screen.height*0.2' a figure

is
>>displayed properly. Why doesnt it work when I have a minus in the

equation.
>>Im not experienced with javascript so excuse if this is a basic question

thanks
ian


Dec 3 '06 #7

P: n/a
Lee
mantrid said:
>
Thanks
>You really shouldn't be abusing he side-effect of the javascript:
pseudo-protocol that way, but if you must, you should have the
event handler return false to prevent the page from reloading
each time the link is clicked:

excuse my ignorance but can you clarify this. why is it abuse?
First I need to point out that my advice to add "return false"
was incorrect. I was thinking of something else when I wrote
that. I'm getting old.

The intention of the javascript: pseudo-protocol is that the
Javascript expression will be evaluated and the resulting HTML
will replace the current contents of the page. What you're
doing is exploiting the side-effect. Your code doesn't return
any HTML, so the current contents are not replaced.

It's abuse because the browser is still expecting to load the
returned contents as a new page. This means that if you make
a little mistake that causes a value to be returned, you're
going to be back here asking why clicking this link replaces
your page with one that contains nothing but the string
"[Object]", or "[Window]", etc, and because certain other
operations cause unpredictable results in some browsers when
the browser thinks that it's about to load new content.
it's about to
--

Dec 3 '06 #8

P: n/a
manxomfoe wrote on 03 dec 2006 in comp.lang.javascript:
Your statement is being confused by something called operator
presidence.
Indeed, but it should be "operator precedence".
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dec 3 '06 #9

P: n/a
manxomfoe wrote:
>Your statement is being confused by something called operator
presidence.
It's "precedence".

--
Bart.
Dec 3 '06 #10

P: n/a
In comp.lang.javascript message
<9T*******************@twister.nyroc.rr.com>, Sun, 3 Dec 2006 17:32:53,
mick white <mi**@mickweb.comwrote:
>
Why not:
alert('Your ideal estimated height is '+screen.height*0.53)
?
screen.height*0.55-screen.height*0.2==screen.height*0.53, no?
Because 0.55 - 0.2 = 0.35 ?

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Dec 3 '06 #11

P: n/a
Dr J R Stockton wrote:
In comp.lang.javascript message
<9T*******************@twister.nyroc.rr.com>, Sun, 3 Dec 2006 17:32:53,
mick white <mi**@mickweb.comwrote:
>>
Why not:
alert('Your ideal estimated height is '+screen.height*0.53)
?
screen.height*0.55-screen.height*0.2==screen.height*0.53, no?


Because 0.55 - 0.2 = 0.35 ?
Oops, but my point is that the subtraction is pointless...
Mick

Dec 3 '06 #12

P: n/a
The formula was just some initial thing i tried while working on something.
Im no maths wizz. perhaps someone can help.
The problem is I have a display screen that I wish to fit exactly
(vertically) to the users monitor without the need to scroll, but different
resolutions will mean what will fit with one will not with another. My
solution (because Im no wizz kid) is that the portions of the display screen
are in a table and changing the height of a vertical marquee in one of the
columns will resize everything else to fit if the user alters its height.
click link to see example
http://www.iddsoftware.co.uk/announc...eenexample.php

I wish to use the alert to display an estimated height for the marquee based
on the screen height. I initially used screen.height*0.55 which works for
some resolutions but when the resolution gets down to say 640x480 it is too
high. I think this has to do with the fact that the menu bars and tool bars
of the browser take up an increasingly larger percentage of the screen as
the resolution becomes lower. so I want to incorporate a factor into the
equation that will compensate for this.
Any ideas?
Ian
"mick white" <mi**@mickweb.comwrote in message
news:0F*******************@twister.nyroc.rr.com...
Dr J R Stockton wrote:
In comp.lang.javascript message
<9T*******************@twister.nyroc.rr.com>, Sun, 3 Dec 2006 17:32:53,
mick white <mi**@mickweb.comwrote:
>
Why not:
alert('Your ideal estimated height is '+screen.height*0.53)
?
screen.height*0.55-screen.height*0.2==screen.height*0.53, no?

Because 0.55 - 0.2 = 0.35 ?

Oops, but my point is that the subtraction is pointless...
Mick

Dec 4 '06 #13

P: n/a
mantrid wrote:
The formula was just some initial thing i tried while working on something.
Im no maths wizz. perhaps someone can help.
The problem is I have a display screen that I wish to fit exactly
(vertically) to the users monitor without the need to scroll, but different
resolutions will mean what will fit with one will not with another.
You are making a huge assumption, namely that screen size and window
size are the same. I would posit that the typical Mac user has his
window size set to something less than screen size.

So your carefully calculated algorithms will be unsatisfactory. You
could determine the browser's window size, and act accordingly. IE,
however, will not expose this information until the window is fully
loaded (this may have changed with IE7, I don't know);

onload= function(){
winWidth=innerWidth||document.clientWidth;
winHeight=innerHeight||document.clientHeight;
//do something with height/width
}
Not tested.
Mick
Dec 4 '06 #14

P: n/a

mantrid wrote:
The formula was just some initial thing i tried while working on something.
Im no maths wizz. perhaps someone can help.
Please don't top-post, reply below trimmed quotes.
The problem is I have a display screen that I wish to fit exactly
(vertically) to the users monitor without the need to scroll, but different
resolutions will mean what will fit with one will not with another.
There are a great many factors that mean attempting to "fit" content to
a users screen based on the reported pixel dimensions will not work
reliably, or even to fit it within the reported window dimensions.
Even if you account for the vaguaries of toolbars, scroll bars, etc.
you can't know what fonts or font sizes are being used - they may be
different to what you've specified and aren't consistent on different
platforms anyway.

Give it up.

This isn't a javascript issue, it is a a fundamental page design issue
and is better discussed in a HTML or CSS group. You will get better
answers there - be prepared for some criticism of what you are trying
to do, but try to see through that.

Ensure your markup is valid first:

<URL:
http://validator.w3.org/check?uri=ht...eenexample.php
>
news:comp.infosystems.www.authoring.html
<URL:
http://groups.google.com.au/group/co...authoring.html >

news:comp.infosystems.www.authoring.stylesheets
<URL:
http://groups.google.com.au/group/co...ng.stylesheets
>

--
Rob

Dec 4 '06 #15

P: n/a
In comp.lang.javascript message
<kY*****************@newsfe2-win.ntli.net>, Mon, 4 Dec 2006 00:28:00,
mantrid <ia********@virgin.netwrote:
>The formula was just some initial thing i tried while working on something.
Im no maths wizz. perhaps someone can help.
The problem is I have a display screen that I wish to fit exactly
When posting, as well as neither top-posting nor over-quoting, please
set your right margin to about 72 characters; it is the norm. If that
requires you to compose in fixed-pitch, I would not be surprised.

<FAQENTRYThat (and not allowing code-wrap) should be in FAQ 3.2,
though I don't see it there at present.

It's a good idea to read the newsgroup and its old FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/ Old RC FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Dec 4 '06 #16

P: n/a
Dr J R Stockton said the following on 12/4/2006 5:53 PM:
In comp.lang.javascript message
<kY*****************@newsfe2-win.ntli.net>, Mon, 4 Dec 2006 00:28:00,
mantrid <ia********@virgin.netwrote:
>The formula was just some initial thing i tried while working on
something.
Im no maths wizz. perhaps someone can help.
The problem is I have a display screen that I wish to fit exactly

When posting, as well as neither top-posting nor over-quoting, please
set your right margin to about 72 characters; it is the norm. If that
requires you to compose in fixed-pitch, I would not be surprised.

<FAQENTRYThat (and not allowing code-wrap) should be in FAQ 3.2,
though I don't see it there at present.
How to post to Usenet in a Javascript FAQ. How intuitive.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 5 '06 #17

P: n/a
mantrid wrote:
Thanks
Thanks to whom? Please include an attribution line, and quote the minimum
of what is required for preserving the context:
>><a href="javascript:alert('Your ideal estimated height is '+
screen.height*0.55-screen.height*0.2);" class="BodyLink">Click to
calculate height </a>

You really shouldn't be abusing he side-effect of the javascript:
pseudo-protocol that way, but if you must, you should have the
event handler return false to prevent the page from reloading
each time the link is clicked:

excuse my ignorance but can you clarify this. why is it abuse?
Because that pseudo-protocol or URI scheme was not designed (by Netscape)
simply to execute code, but to evaluate code and create a temporary HTML
document from the result and display it.

Since alert() does return `undefined', no such document is created with
javascript:alert(...). However, since that is not standardized (hence
called "proprietary"),

1. you cannot rely on that there is no temporary document created and
displayed,

2. you cannot rely on that this is even supported (you risk an error
message), and

3. it is known that since the target URL of the view could be considered to
change for the UA (most notably MS Internet Explorer), all kinds of weird
behavior can occur, such as animations suddenly stopping.

See the FAQ. (This list should be appended/merged into it.)
PointedEars
--
I hear, and I forget; I see, and I remember; I do, and I understand.
-- Chinese proverb
Dec 9 '06 #18

P: n/a
Thomas 'PointedEars' Lahn wrote:
<snip>
with javascript:alert(...). However, since that is not
standardized (hence called "proprietary"),

1. you cannot rely on that there is no temporary document
created and displayed,

2. you cannot rely on that this is even supported (you
risk an error message), ...
Or that where supported its behaviour will be consistent. For example,
IceBrowser 5 would execute code in a javascript pseudo-protocol HREF but
would not replace the current document even if the result of that
execution were a string of HTML. An implementation based upon observing
the protocol's use for its side effects but failing to perceive its full
behaviour/purpose.

Richard.
Dec 9 '06 #19

P: n/a
In comp.lang.javascript message <36****************@PointedEars.de>,
Sat, 9 Dec 2006 14:15:44, Thomas 'PointedEars' Lahn <Po*********@web.de>
wrote:
>mantrid wrote:
>Thanks

Thanks to whom? Please include an attribution line, and quote the minimum
of what is required for preserving the context:
Ja, Herr UberKraut. Heil Dummkopf Lahn!

Please include in your articles an adequate attribution line, sufficient
to identify the article being cited and its date.

In the "DE." hierarchy, the nominal rules may differ from those of the
Big-8; but (1) this newsgroup is not in the "DE." hierarchy, (2)
adequate attributions are common there, in spite of your previous
bullying.
>PointedEars

Your signature is STILL not compliant with established Usenet
convention. Read FYI28 until you understand it.

--
(c) John Stockton, Surrey, UK. ??*@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.
Dec 9 '06 #20

This discussion thread is closed

Replies have been disabled for this discussion.