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.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    TEXAS . . . 'nuf said
    Posts
    468
    Thanks
    0
    Thanked 0 Times in 0 Posts

    goToAnchor JS adjustment [EASY for you (?)]

    I'm having trouble with this again.

    I know the JS needs to be modified, but nothing I've tried works.

    I need to have anchors for the months (01, 02, 03...10, 11, 12). I don't want to have to change any years nor days! The anchors need to be exactly as they are below in this example. Do not assume that all months will have anchors. Do not assume that the months will start with January and continue accordingly. This is the reason we use two digits. As it is right now, September is the first month...progressing through May or so.

    Please adjust this JS accordingly for me! I really need the help...my mind is SPENT on too many other problems right now.

    This whole bit of code goes in between the <BODY> and </BODY>....NOT in the <head>.


    <BODY>
    <script language="javascript">

    function goToAnchor(){
    var today = new Date();
    var mm = today.getMonth()+1;
    var dd = today.getDate();
    var yy = today.getFullYear();

    if (mm<10) mm = "0" + mm;
    if (dd<10) dd = "0" + dd;

    anchorname = "" + yy + mm + dd;

    location.hash = findAnchor(anchorname)
    }

    function findAnchor(x){
    a = document.anchors
    for(i=0; i<a.length; i++)
    if(x<=a[i].name) return a[i].name
    return a[i-1].name
    }

    window.onload=goToAnchor
    </script>
    <A name=09></A>SEPTEMBER
    .

    <A name=10></A>OCTOBER
    .
    BLAH BLAH BLAH
    .
    <A name=01></A>JANUARY
    .
    BLAH BLAH BLAH
    .
    <A name=07></A>JULY
    </BODY>
    Gordo
    "In the End, we will remember not the words of our enemies, but the silence of our friends."
    - Martin Luther King Jr. (1929-1968)

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    TEXAS . . . 'nuf said
    Posts
    468
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow, only 10 views at the time of this post!? Why so low? Well, you're reading this now, so do you have a solution?
    Gordo
    "In the End, we will remember not the words of our enemies, but the silence of our friends."
    - Martin Luther King Jr. (1929-1968)

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Instead of

    window.onload=goToAnchor

    have you tried

    window.onload=goToAnchor()

    ??

    a function call has to have those parens...
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    TEXAS . . . 'nuf said
    Posts
    468
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply, but what you suggest is incorrect. See http://www.javascriptkit.com/javatut...taction4.shtml .

    The script jumps, but not to the right place. The JS code is looking for something like <a name=20020928></a> for, say, September 28, 2002. I need the JS adjusted to merely look for the current month -- no day and no year. It needs to look for 01, 02, 03, ... , 09, 10, 11, 12. I need it to take the month from the users' computer and figure go to the corresponding month.

    This is the best I could do, but it's not working quite right. It jumps of course, but not to the correct location. It keeps starting with the first anchor in my page ... which is "09" for September.


    <script language="javascript">

    function goToAnchor(){
    var today = new Date();
    var mm = today.getMonth()+1;

    if (mm<10) mm = "0" + mm;

    anchorname = "" + mm;

    location.hash = findAnchor(anchorname)
    }

    function findAnchor(x){
    a = document.anchors
    for(i=0; i<a.length; i++)
    if(x<=a[i].name) return a[i].name
    return a[i-1].name
    }

    window.onload=goToAnchor
    </script>


    Is the if (mm<10) mm = "0" + mm; the problem? Do computer dates already use two digits for the month...or (more likely) does this change based on the user's settings. Maybe it should look for two digits instead of a number <10.

    This is just slightly over my head.
    Last edited by Gordo; 01-11-2003 at 10:14 PM.
    Gordo
    "In the End, we will remember not the words of our enemies, but the silence of our friends."
    - Martin Luther King Jr. (1929-1968)

  • #5
    Regular Coder
    Join Date
    Sep 2002
    Location
    Louisiana
    Posts
    576
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Gordo
    a = document.anchors
    it should be
    Code:
    a = document.anchor
    my reference is
    http://packetstormsecurity.nl/progra...m#anchorsarray

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    TEXAS . . . 'nuf said
    Posts
    468
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmmm...thanks for the reference. It's not too common that folks link such info.

    But, changing it to just anchor gives me a JS error of 'a.length' is null or not an object.

    I also noticed that in the EXAMPLE just below the description has the following:
    if (document.anchors.length == 0) {

    Last edited by Gordo; 01-12-2003 at 01:33 AM.
    Gordo
    "In the End, we will remember not the words of our enemies, but the silence of our friends."
    - Martin Luther King Jr. (1929-1968)

  • #7
    Regular Coder
    Join Date
    Sep 2002
    Location
    Louisiana
    Posts
    576
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah, i realized that after i posted. sorry for the bad infor
    what kind of errors are you getting on that code?

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Location
    TEXAS . . . 'nuf said
    Posts
    468
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Confused...error on YOUR code or MINE? I posted the error I got when I took out the "s".

    As for my code, I don't get an error. It just doesn't jump to the correct location. It's most likely a problem with how the months (on the user's computer) are read by the JS. Other than that...well....that's why I'm here -- because I don't know.
    Gordo
    "In the End, we will remember not the words of our enemies, but the silence of our friends."
    - Martin Luther King Jr. (1929-1968)

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually what I posted was not incorrect, if you are trying to call a function - and by the code you posted:

    function goToAnchor(){
    ...........

    It's quite obvious that "goToAnchor()" is indeed a function.

    It looks to me like there is a typo (or oversight) in the tutorial you're referencing, since they mistakenly left out necessary parentheses, as I stated above.

    You need, and let me emphasize this yet again:

    window.onload=goToAnchor();

    When calling a function in javascript, the parentheses are a necessary part of the syntax, otherwise javascript thinks you're referencing a variable...

    Did I mention that when calling a function, you have to use parentheses? I know this from my own painful javascript experiences...

    Next time instead of assuming someone is incorrect, it can't hurt a thing to just try it out - you might learn something. If that doesn't fix your script, it will get you part of the way there. You can insert some alert() inside the function to make sure it's being called.

    Not trying to be harsh, but it's a bit frustrating when you give someone a fix to their problem (at least partial, haven't tested the rest of the script, but that was a glaring error), and they totally blow it off without even trying it or researching it... and then tell you you're incorrect...
    Last edited by whammy; 01-12-2003 at 06:18 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #10
    Regular Coder
    Join Date
    Jun 2002
    Location
    TEXAS . . . 'nuf said
    Posts
    468
    Thanks
    0
    Thanked 0 Times in 0 Posts
    GOTCHA!

    Okay, so my attempt at making this script work has the following JS:

    <script language="javascript">

    function goToAnchor(){
    var today = new Date();
    var mm = today.getMonth()+1;

    if (mm<10) mm = "0" + mm;

    anchorname = "" + mm;

    location.hash = findAnchor(anchorname)
    }

    function findAnchor(x){
    a = document.anchors
    for(i=0; i<a.length; i++)
    if(x<=a[i].name) return a[i].name
    return a[i-1].name
    }

    window.onload=goToAnchor()
    </script>


    The original Owl (CodingForums user) script is below:

    <script language="javascript">

    function goToAnchor(){
    var today = new Date();
    var mm = today.getMonth()+1;
    var dd = today.getDate();
    var yy = today.getFullYear();

    if (mm<10) mm = "0" + mm;
    if (dd<10) dd = "0" + dd;

    anchorname = "" + yy + mm + dd;

    location.hash = findAnchor(anchorname)
    }

    function findAnchor(x){
    a = document.anchors
    for(i=0; i<a.length; i++)
    if(x<=a[i].name) return a[i].name
    return a[i-1].name
    }

    </script>
    <body onload="goToAnchor()">


    But I need to have the goToAnchor() inside the JS. No problem...I think it's right in the blue example above (with Whammy's ()).

    With my blue example above, I get the following JS error:
    'a[...].name' is null or not an object
    (see attachment for screenshot of pop-up error message)

    <EDIT>
    As for blowing you off, Whammy, I didn't. By default, I trust a tutorial over an individual's post (mod or not). I don't claim to know JavaScript. Before inserting the (), the script would jump to the first anchor. That led me to believe that it was working (the jump part at least), but that it wasn't finding the correct location. No JS errors appeared at that time. It was only after adding the () that I got the JS error message. So, I assumed (poorly) that the problem was the added (). The tutorial merely substantiated my thoughts...or so I thought. I'm not here to frustrate anyone. I do my best to help others...and give complete info in my own queries. All is good. I just need to figure this JS issue out.
    </EDIT>
    Attached Thumbnails Attached Thumbnails goToAnchor JS adjustment [EASY for you (?)]-gotoanchor_error.gif  
    Last edited by Gordo; 01-12-2003 at 06:26 AM.
    Gordo
    "In the End, we will remember not the words of our enemies, but the silence of our friends."
    - Martin Luther King Jr. (1929-1968)

  • #11
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok... now we're getting somewhere!

    I could be wrong, too - I'm not the be-all and end all of javascript, but I just know in my experience, a function call has never ever worked without parentheses... that it did ANYTHING without parentheses is a bit shocking to me, and makes me wonder.

    I'm a bit confused about this part of the script:

    Code:
    function findAnchor(x){ 
    a = document.anchors 
    for(i=0; i<a.length; i++) 
    if(x<=a[i].name) return a[i].name 
    return a[i-1].name 
    }
    Perhaps because I wouldn't do it like that... but what you can do is check to see what these "names" are, but sticking an alert() inside this function in key places, so you can see exactly what value you are getting there, i.e.:

    Code:
    function findAnchor(x){ 
       var a = document.anchors;
       for(i=0; i<a.length; i++) {
          alert(a[i].name);
          if(x<=a[i].name) {
             return a[i].name;
             return a[i-1].name //This doesn't look like it should be in the loop ?!
          }
       }
    }
    Welcome to debugging 101.

    P.S. I'm not totally sure whether all browsers support document.anchors or not, I'll need to research it a bit.

    This might be an interesting thing to fix, though.
    Last edited by whammy; 01-12-2003 at 06:35 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #12
    Regular Coder
    Join Date
    Jun 2002
    Location
    TEXAS . . . 'nuf said
    Posts
    468
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm...I don't know how all the code works...just that it does. The example in red (post before this one) works beautifully in N4.x, N6.x, Mozilla1.x, Opera6.x, and IE --- all the major browsers. Owl wrote it in one of my other posts several months back. Actually, Whammy, you replied to the code with the following:
    I dunno... but that was simplicity at its finest. I think we can all appreciate that. I know i can.
    I just know the solution to getting it to look for a simple month date should be simple...even though the months are not in order. That shouldn't matter IMO. It's a matter or reading the month on the user's computer and looking for the two digit counterpart.

    FYI...I left a "PM" for Owl a few days ago asking him to stop by this thread. I just tried to email him, but this feature is not active in his profile. He hasn't posted here in a month!
    Last edited by Gordo; 01-12-2003 at 08:44 AM.
    Gordo
    "In the End, we will remember not the words of our enemies, but the silence of our friends."
    - Martin Luther King Jr. (1929-1968)

  • #13
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Originally posted by whammy
    I could be wrong, too - I'm not the be-all and end all of javascript, but I just know in my experience, a function call has never ever worked without parentheses... that it did ANYTHING without parentheses is a bit shocking to me, and makes me wonder.
    window.onload = myFunc();

    is correct only if myFunc() returns a function.

    i.e.
    function myFunc() {
    return function() { alert('hi!') };
    }

    Assignment is evaluated from right to left, therefore whatever myFunc() returns is set to window.onload... it could be "apple" for all we know.

    When the page loads, essentially:

    window.onload() is called.

    For that to work, it NEEDS to be assigned a reference to a function.

  • #14
    Owl
    Owl is offline
    New Coder
    Join Date
    Jun 2002
    Posts
    62
    Thanks
    0
    Thanked 1 Time in 1 Post
    Hi Gordo,
    Code:
    <script language="javascript"> 
    
    function goToAnchor(){ 
    var today = new Date(); 
    var mm = today.getMonth()+1; 
    
    location.hash = findAnchor(mm) 
    } 
    
    function findAnchor(x){ 
    da = document.anchors
    a=[]
    for(j=0; j<da.length; j++) a[j]=da[j].name
    a.sort()
    for(j=0; j<a.length; j++) 
    if(x<=a[j]) return a[j]
    return a[j-1]
    } 
    
    window.onload=goToAnchor //no parentheses
    </script>
    Originally posted by Gordo
    FYI...I left a "PM" for Owl a few days ago asking him to stop by this thread...
    You have a better chance of getting me faster in my regular forum.

    Hi wammy,

    All browsers support document.anchors.

    ( •) (• )
    >>V

  • #15
    Regular Coder
    Join Date
    Jun 2002
    Location
    TEXAS . . . 'nuf said
    Posts
    468
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just wanted to thank all who put in their $0.02 on this one. Owl's solution works flawlessly ... again!
    Gordo
    "In the End, we will remember not the words of our enemies, but the silence of our friends."
    - Martin Luther King Jr. (1929-1968)


  •  

    Posting Permissions

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