473,320 Members | 1,872 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,320 software developers and data experts.

Can't sum variables

106 100+
I have this code which retrives the anchor value from the url.
(www.example.com/page#topic1 > gives 'topic1' )

Expand|Select|Wrap|Line Numbers
  1. var anchorValue;
  2. var url = document.location;
  3. var strippedUrl = url.toString().split("#");
  4. if(strippedUrl.length > 1)
  5. anchorvalue = strippedUrl[1];
I want to do a very simple thing, add, for example '10' to the anchorvalue variable.

So if we have www.example.com/page#20 the anchorvalue gives '20' and I want to increment that value in '10' so that I get '30'.


Expand|Select|Wrap|Line Numbers
  1. var anval = anchorvalue;
  2. var sumvalue = 10;
  3. var newanchorvalue = anval + sumvalue;
But that gives 2010 and not 30!

How can I achieve what I want ?

Thanks in advance.
Aug 18 '10 #1

✓ answered by Dormilich

I have this code which retrives the anchor value from the url.
Expand|Select|Wrap|Line Numbers
  1. window.location.hash
?

But that gives 2010 and not 30!

How can I achieve what I want ?
that gives 2010 because the + operator performes both addition and string concatenation. if the first operand is a string, it will do concatenation, if it is a number, it will do addition. thus you have to cast your variable to a number, of which different ways are possible:
- use the Number constructor Number(val)
- use a mathematical operation val * 1 or val - 0
- use a conversion function parseInt()/parseFloat()

8 2162
Dormilich
8,658 Expert Mod 8TB
I have this code which retrives the anchor value from the url.
Expand|Select|Wrap|Line Numbers
  1. window.location.hash
?

But that gives 2010 and not 30!

How can I achieve what I want ?
that gives 2010 because the + operator performes both addition and string concatenation. if the first operand is a string, it will do concatenation, if it is a number, it will do addition. thus you have to cast your variable to a number, of which different ways are possible:
- use the Number constructor Number(val)
- use a mathematical operation val * 1 or val - 0
- use a conversion function parseInt()/parseFloat()
Aug 18 '10 #2
londres9b
106 100+
window.location.hash gives #anchor and I want just anchor ...

I know why it gives 2010 but as I know very little of Javascript, I can't make anchorvalue a number.
Aug 18 '10 #3
Dormilich
8,658 Expert Mod 8TB
window.location.hash gives #anchor and I want just anchor ...
I assume, you are able to remove the first character, ain’t you?

I can't make anchorvalue a number.
I told you how to do that
Aug 18 '10 #4
londres9b
106 100+
I got it, I multiplied the var by 1, like you suggested.

Thank you very much :D

Here's the code:

Expand|Select|Wrap|Line Numbers
  1. var anchorValue;
  2. var url = document.location;
  3. var strippedUrl = url.toString().split("#");
  4. if(strippedUrl.length > 1)
  5. anchorvalue = strippedUrl[1];
  6.  
  7. var anchorNumber = anchorvalue * 1;
  8.  
  9. var increment = 10;
  10.  
  11. var result = anchorNumber+increment;
Aug 18 '10 #5
Dormilich
8,658 Expert Mod 8TB
you could also have switched increment and anchorNumber, since increment is already a number.

PS.
Expand|Select|Wrap|Line Numbers
  1. var anchorValue = window.location.hash.substr(1);
Aug 18 '10 #6
londres9b
106 100+
I assume, you are able to remove the first character, ain’t you?
Why? I know very little of JavaScript..

Expand|Select|Wrap|Line Numbers
  1. var anchorNumber = window.location.hash.substr(1);
Thanks :)

you could also have switched increment and anchorNumber, since increment is already a number.
Like this right?
Expand|Select|Wrap|Line Numbers
  1. var anchorNumber = anchorvalue * 1;
  2. var result = anchorNumber+10;
  3.  
Aug 18 '10 #7
Dormilich
8,658 Expert Mod 8TB
Like this right?
nope. (remember what I said about the type of the first operand)

Expand|Select|Wrap|Line Numbers
  1. var result = 10 + anchorNumber;
Aug 18 '10 #8
londres9b
106 100+
Ok,

Many Thanks.
Aug 18 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

23
by: Mark Parnell | last post by:
I'm relatively new to PHP, and have just converted a site from ASP to PHP. There is one thing I haven't managed to do, though. When the site was using ASP, I had one file (called variables.asp),...
7
by: Nicole | last post by:
Hi I'm trying to use a function to set a session variable. I have three files: The first file has: <?php session_start(); // This connects to the existing session ?> <html> <head>
2
by: Newbie | last post by:
Hi All, I would really appreciate any ideas / clues on this issue which I am facing. I have a asp.net application, the problem is with maintaining session variables while using authentication for...
11
by: Matt Mercer | last post by:
Hi, I have a simple question about vb .net variables. There is so much info on variables out there I had trouble finding exactly what I am looking for. Here is the question: When I use a...
14
by: Coleen | last post by:
Hi All :-) We have an APSX application using VB.net as the code behind, which uses one or two session variables per page. These Session variables are passed to the final page and calculations...
5
by: puzzlecracker | last post by:
does it only applies to member variables?
12
by: Charlie | last post by:
I have a file, data.c, where I define all of my global variables. I then have a header file, data.h, which I include in every file in which I reference all of the variables defined in data.c. ...
1
by: sandhya rani | last post by:
Hai forum members, Can we store cursor variables in PL/SQL.According to me the answer is NO. Because a cursor variable points a row which can't be...
5
by: RamyaSrinath | last post by:
please tell me that if a session variable is not initialised either in global.asa or anywhere else and has to take user information, how can this be possible? please explain me with code. I have...
7
by: NDayave | last post by:
How do, I want to be able to make a certain number of variables depending on the number of data items i have to be used. For example, i would need 3 variables defined when i have 3 numbers and...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.