473,508 Members | 2,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using a minus in javascript

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
19 8368
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
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
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
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
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
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
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
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
manxomfoe wrote:
>Your statement is being confused by something called operator
presidence.
It's "precedence".

--
Bart.
Dec 3 '06 #10
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
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
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
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

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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13826
by: Newbie | last post by:
Hi, Could someone please tell how MINUS operator works for comparing and giving non-matching records in Table1? Does MINUS operator compares all records of Table1 with all records of Table2 to...
7
2964
by: johkar | last post by:
I am confused on childNodes or children. When you have nested lists, I would think it would all be one list in the Dom, this doesn't seem to be the case. So how do you traverse an unordered list?...
1
13992
by: Rob | last post by:
I have a date text box (input type = text) in an ASP.NET/Javascript environment. I wanted to force the users to enter dates in a "__/__/____", "dd/mm/yyyy" or similar format. The textbox needs to...
5
2239
by: Jumbo | last post by:
I need a script for my site that shows my servertime minus 7 hours. Anyone who can help?
61
4865
by: Christoph Zwerschke | last post by:
On the page http://wiki.python.org/moin/Python3%2e0Suggestions I noticed an interesting suggestion: "These operators ≤ ≥ ≠ should be added to the language having the following meaning: ...
16
2006
by: Giggle Girl | last post by:
Hi there, I have a nav tree similar to the XP Windows Explorer in behavior. It uses styles to underline a row on mouseover, and change the look of a row when clicked. Currently, it is working...
4
11536
by: tamnguyet | last post by:
I have n days and a date, I want a date plus(or minus) to n days.How can I do that in javascript
2
2272
by: Bluesangig | last post by:
Hi all, I am trying to append a TABLE element (being created dynamically) to an existing DIV element in a HTML document. But when trying to do so it is giving me the following error: Line: 141...
0
1254
by: thomas.mertes | last post by:
Generally C is well suited as target language for compilers. I use C as target language for the Seed7 compiler. This works good. There are just some things where the behaviour of C is undefined: ...
0
7224
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
7120
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
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7380
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
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4706
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...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.