Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Sep 2002
    Location
    New York
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to insert a calculated value into an <a href..> tag

    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.

  • #2
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    <a name="theLink" href = "np/np44.htm" >click here</a>

    document.links['theLink'].href = "whatever you want your link to be.html"
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #3
    New Coder
    Join Date
    Oct 2002
    Location
    The Netherlands
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    option 1:
    You can do something like this
    Code:
    <script>
    document.write("<a href = 'np/np"+weekNo+".htm' >");
    <script>
    click here
    </a>
    option 2:
    Or give the link an id like this
    Code:
    <a href="" id="mylink"> click here</a>
    and then change the href property of the link like this
    Code:
    <script>
    document.all.mylink.href="np/np"+weekNo+".htm'";
    </script>
    hope this helps

  • #4
    New Coder
    Join Date
    Sep 2002
    Location
    New York
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    doesn't work; still need help

    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.

  • #5
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #6
    New Coder
    Join Date
    Sep 2002
    Location
    New York
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    sorry, i just don't understand

    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.

  • #7
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    simple:
    Code:
    <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
    Last edited by Vladdy; 11-01-2002 at 09:24 PM.
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #9
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    or you could just do what Vladdy suggested. his way works too.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #10
    New Coder
    Join Date
    Sep 2002
    Location
    New York
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    bless you vladdy; it works.

    thank you vladdy.
    i don't inderstand it yet, but it works and i can adapt it to everything i need.
    bless you.

  • #11
    New Coder
    Join Date
    Sep 2002
    Location
    New York
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    works on my pc but not on my host

    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.
    Last edited by bcbasslet; 11-02-2002 at 02:02 AM.

  • #12
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #13
    New Coder
    Join Date
    Sep 2002
    Location
    New York
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    tried it; doesn't work; here's the code

    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?
    Last edited by bcbasslet; 11-02-2002 at 04:35 AM.

  • #14
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can you post a link to your site?
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #15
    New Coder
    Join Date
    Sep 2002
    Location
    New York
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    sure!

    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •