...

View Full Version : goToAnchor JS adjustment [EASY for you (?)]



Gordo
01-10-2003, 06:24 AM
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
01-11-2003, 06:01 AM
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?

whammy
01-11-2003, 08:13 PM
Instead of

window.onload=goToAnchor

have you tried

window.onload=goToAnchor()

??

a function call has to have those parens...

Gordo
01-11-2003, 11:10 PM
Thanks for the reply, but what you suggest is incorrect. See http://www.javascriptkit.com/javatutors/eventaction4.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. :confused: :confused:

This is just slightly over my head. :o

chrismiceli
01-12-2003, 01:19 AM
Originally posted by Gordo
a = document.anchors

it should be


a = document.anchor

my reference is
http://packetstormsecurity.nl/programming-tutorials/JavaScript/javascript-ref.htm#anchorsarray

Gordo
01-12-2003, 02:30 AM
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) {

:confused:

chrismiceli
01-12-2003, 04:01 AM
yeah, i realized that after i posted. sorry for the bad infor :(
what kind of errors are you getting on that code?

Gordo
01-12-2003, 04:06 AM
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.

whammy
01-12-2003, 06:49 AM
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... :eek:

Gordo
01-12-2003, 07:18 AM
GOTCHA! :D

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>

whammy
01-12-2003, 07:27 AM
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:



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.:



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.

Gordo
01-12-2003, 09:40 AM
Hmm...I don't know how all the code works...just that it does. The example in red (post before this one) works beautifully (http://www.bowiefootball.org/calendar.htm) in N4.x, N6.x, Mozilla1.x, Opera6.x, and IE --- all the major browsers. Owl wrote it in one of my other posts (http://www.codingforums.com/showthread.php?s=&threadid=6682&highlight=anchor+date) 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! :confused:

jkd
01-12-2003, 05:54 PM
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.

Owl
01-17-2003, 02:37 AM
Hi Gordo,
<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 (http://www.webxpertz.net/forums/forumdisplay.php3?forumid=1).

Hi wammy,

All browsers support document.anchors.

( ) ( )
>>V

Gordo
01-21-2003, 04:01 AM
Just wanted to thank all who put in their $0.02 on this one. Owl's solution works flawlessly ... again!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum