I have this code which retrives the anchor value from the url.
( www.example.com/page#topic1 > gives 'topic1' ) -
var anchorValue;
-
var url = document.location;
-
var strippedUrl = url.toString().split("#");
-
if(strippedUrl.length > 1)
-
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'. - var anval = anchorvalue;
-
var sumvalue = 10;
-
var newanchorvalue = anval + sumvalue;
But that gives 20 10 and not 30!
How can I achieve what I want ?
Thanks in advance.
I have this code which retrives the anchor value from the url.
?
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
I have this code which retrives the anchor value from the url.
?
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()
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.
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
I got it, I multiplied the var by 1, like you suggested. Thank you very much :D
Here's the code: - var anchorValue;
-
var url = document.location;
-
var strippedUrl = url.toString().split("#");
-
if(strippedUrl.length > 1)
-
anchorvalue = strippedUrl[1];
-
- var anchorNumber = anchorvalue * 1;
-
-
var increment = 10;
-
-
var result = anchorNumber+increment;
-
you could also have switched increment and anchorNumber, since increment is already a number.
PS. - var anchorValue = window.location.hash.substr(1);
I assume, you are able to remove the first character, ain’t you?
Why? I know very little of JavaScript.. - 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? -
var anchorNumber = anchorvalue * 1;
-
var result = anchorNumber+10;
-
Like this right?
nope. (remember what I said about the type of the first operand) - var result = 10 + anchorNumber;
Sign in to post your reply or Sign up for a free account.
Similar topics
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),...
|
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>
|
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...
|
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...
|
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...
|
by: puzzlecracker |
last post by:
does it only applies to member variables?
|
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.
...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
| |