PDA

View Full Version : how to insert a calculated value into an <a href..> tag


bcbasslet
11-01-2002, 05:10 AM
I need to link to a page that is named for the current week of the year.
I've got the week number (thanks to this site!)
Lets say its weekNo=44
How to i write the syntax to get that 44 into my href

<a href = "np/np44.htm" >click here</a>

I've tried all the string concatenation things i can think of.
I've tried eval on weekNo...i just don't know how.
thanks.

joh6nn
11-01-2002, 06:43 AM
<a name="theLink" href = "np/np44.htm" >click here</a>

document.links['theLink'].href = "whatever you want your link to be.html"

aEr_aEr
11-01-2002, 06:47 AM
option 1:
You can do something like this

<script>
document.write("<a href = 'np/np"+weekNo+".htm' >");
<script>
click here
</a>


option 2:
Or give the link an id like this

<a href="" id="mylink"> click here</a>

and then change the href property of the link like this

<script>
document.all.mylink.href="np/np"+weekNo+".htm'";
</script>


hope this helps

bcbasslet
11-01-2002, 08:51 PM
the document.write method isn't what i want to do.

i want to get the pagename into the <a> tag under a click.

the document.all.mylink.href="np/np"+weekNo+".htm'" ; gives an error message "document.all.mylink.hfref is null or not an object

here's my code:

<html>
<head>
<title>New Page 1</title>
<script>
weekNo = 44 ; // substitute js later
document.all.mylink.href="np/np"+weekNo+".htm'" ;
</script>
</head>
<body>

<a href="" id="mylink"> click here</a>
</body>
</html>

Please help.
thanks.

joh6nn
11-01-2002, 09:03 PM
your problem arises from the fact that the script is trying to manipulate the link, before the link has loaded. there are two ways to fix the problem. either, set the script to run when the onload event fires, or, place the script at the end of the page, so that it will run after the link has finished loading.

also, i suggest using document.links['linkName'].href, instead of document.all.linkId.href, because document.all is not cross browser.

bcbasslet
11-01-2002, 09:15 PM
Thank you, but i don't understand. i was just trying to follow the code of the Aer Aer replyer.

I'm sorry but i don't understand what you're saying, and i'm trying.

i have a huge hunk of js that determines the week number.

i want to have a "click here" in my body that links to a page named with that link number.

So my week number script has to run at the top of the page, yes?
Then i dont know how to incorporate your instructions:
<a name="theLink" href = "np/np44.htm" >click here</a>

Its exactly that number 44 that i'm tring to insert into the href.
What does giving a name to the anchor tag do for me?
Please help.
thank you.

joh6nn
11-01-2002, 09:18 PM
if you show me the rest of your code, i'll try to explain in detail, what i'm talking about, and show you how to write the code you need for the link.

Vladdy
11-01-2002, 09:20 PM
simple:

<script>
function composeHref(link)
{ linkhref= //compose the link string here
link.href=linkhref;
return true; //IMPORTANT LINE
}
</script>
....
<a href="#" onclick="return composeHref(this);">Link</a>



Onclick is processed before the redirection occurs so you use the event to compose the href attribute.

BINGO

joh6nn
11-01-2002, 09:21 PM
or you could just do what Vladdy suggested. his way works too.

bcbasslet
11-02-2002, 12:02 AM
thank you vladdy.
i don't inderstand it yet, but it works and i can adapt it to everything i need.
bless you.

bcbasslet
11-02-2002, 01:53 AM
vladdy,
your technique works on my FrontPage win2k pc, but doesn't work when posted to my host.
I get this error:
Error 404: /susan/np/npundefined.htm is not found at this location

when i'm expecting to load the page:
/susan/np/np43.htm

the number we've been calculating comes up 'undefined'

HEre's my exact code:
<script language="JavaScript1.2" src="scripts/weekno.js"></script>
<script>
var weekNo = weekNo() - 1 ; // this works; gives me 43
var weekBack1 = weekNo - 1 ; //this works; gives me 42
var weekBack2 = weekNo - 2 ; //this works; gives me 41
</script>
</head>
<body>
<script>
function composeHref(link,myweek)
{
linkhref= 'np/np' + myweek + '.htm' ;
link.href=linkhref;
return true; //IMPORTANT LINE
}
</script>
<a href="#" onclick="return composeHref(this,weekBack1);">Last week's news</a>
<br><br>
<a href="#" onclick="return composeHref(this,weekBack2);">two weeks ago</a>
</body>
</html>

thanks again, in advance.

joh6nn
11-02-2002, 02:54 AM
var weekNo = weekNo() - 1 ; // this works; gives me 43

you can't name things identically in javascript. come up with a new name for the variable ( the one in red ), and it should work fine.

bcbasslet
11-02-2002, 04:07 AM
When i load the page, i get "error on page" before i try to click anything.
If i replace weekBack1 in the href with an integer, it works FINE.
but that's my point, i'm trying to replace it with a calc'd value.

<script>
var thisweek = weekNo() - 1 ;
var weekBack1 = thisweek - 1 ;
var weekBack2 = thisweek - 2 ;
var weekBack3 = thisweek - 3 ;
var weekBack4 = thisweek - 4 ;
</script>
</head>
<body>
<script>
function composeHref(link,myweek)
{
linkhref= 'np/np' + myweek + '.htm' ;
link.href=linkhref;
return true;
}
</script>

<a href="#" onclick="return composeHref(this,weekBack1);">
Last week
</a>
<br><br>
<a href="#" onclick="return composeHref(this,weekBack2);">
two weeks ago
</a>
<</body>
</html>

----------------------
I found that document.write(weekBack1 + " " + weekBack1.value)
produces 42 and undefined. and 'undefined' is exactly what my error message says! hmmm?

Vladdy
11-02-2002, 04:30 AM
Can you post a link to your site?

bcbasslet
11-02-2002, 05:05 AM
http://www.stroudsburgfoto.com/susan/thisweeksnews.htm

the first 3 links have been hardcoded with integers for the client to see.

the 4th link is coded to use the function. that's the one that doesn't work.

thanks vladdy.

Vladdy
11-02-2002, 07:12 AM
This file can not be found:
<script language="JavaScript1.2" src="scripts/weekno.js"></script>
Hence weekNo is undefined (so are the derived vars)
check path and spelling :D :D

One more advice: download mozilla and use the built-in javascript console - speeds up debugging time by a factor of 10

bcbasslet
11-03-2002, 03:33 AM
:D :D :D i've completely gone nuts.
thank you.
tail between legs.
sulking off into distance
heading toward a labotomy