View Full Version : Jump To Anchor Based On Date (ONLOAD)
Gordo
09-24-2002, 01:36 AM
Anyone care to put together a nice little JavaScript for me!? Working during the day is taking a lot of my time, so I just can't devote as much time as before to researching, experimenting, etc. Plus, I still don't know how to code JavaScript!
Okay, here's what I need. I have a page with a lot of anchors. The page is a calendar of sorts. The anchors are dates in the format of <a name="20020923"> ... for September 23, 2002. I need the JavaScript to determine "today's" date (no matter what day it is), and jump to that day...or the next day if "today" is not an anchor. This way, the visitor is taken to the current/next point of interest onload.Am I missing anything!? I hope that's enough. It should be "down and dirty," but I just don't have the skills to code it. But, I can cut-n-paste with the best of them...so please give me ALL of the code (ready to go)! Thanks!!!
glenngv
09-24-2002, 02:17 AM
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;
var anchorname = "#" + yy + mm + dd;
location.replace(location.href+anchorname);
}
window.onload = gotoAnchor;
Gordo
09-24-2002, 03:32 AM
That looks clean!!!
But it didn't work. The page loads normally...without jumping to the anchors that I inserted.
Strange, because I know glenngv's work is very reliable. I did have to change the "t" to a "T" in the onload statement. I also have my onload in the <body> along with a default status bar.
<head>
<script lanaguage="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;
var anchorname = "#" + yy + mm + dd;
location.replace(location.href+anchorname);
}
</script>
</head>
<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0" onLoad="goToAnchor; window.defaultStatus='My Status Bar Text Goes Here'">
adios
09-24-2002, 03:38 AM
If you're assigning an onload handler in the <body> tag - as opposed to in a JS block - you'll need those parentheses:
onLoad="goToAnchor();
glenngv
09-24-2002, 03:40 AM
<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0" onLoad="goToAnchor(); window.defaultStatus='My Status Bar Text Goes Here'">
if you assign a function to an event handler, you don't need to put paretheses, but if you will call it, you need to put them.
for simplicity, you can make it like this:
function doLoad(){
gotoAnchor();
window.defaultStatus="My Status Bar Text Goes Here";
}
window.onload = doLoad; //no () here
or still use them in the <body> onload attribute. you just have to call it like gotoAnchor()
it's your choice :)
Gordo
09-24-2002, 05:03 AM
In the immortal words of Homer Simpson ----- DOH!
I told you I'd been away from the coding aspect of my hobby, etc. lately. I can't believe I missed that. I mean, I knew that...I really did.
Nonetheless, thanks for pointing out my ommission.
However, I have one thing missing. If "today" is not listed, then I need it to jump to the next available anchor. For example, today is the 23rd. I don't have anything for the 23rd. The next item anchor is, say, the 26th. I need the page to jump to the 26th...to show the next date of importance. Currently, the page just sits there (as before) with calendar.htm#20020923.
adios
09-24-2002, 07:39 AM
Think this works (not very elegant, though)...
<html>
<head>
<script type="text/javascript">
function findAnchor(yy,mm,dd) {
var anc, anchor_name, a = 0, pointer, year, month, day;
while (anc = document.anchors[a++]) {
anchor_name = anc.name;
year = anchor_name.substring(0,4);
if (year == yy) {
month = anchor_name.substring(4,6);
if (month == mm) {
pointer = a - 2;
day = anchor_name.substring(6);
if (day == dd) return anchor_name;
}
}
}
while (anc = document.anchors[pointer++]) {
day = parseInt(anc.name.substring(6));
if (day<parseInt(dd) && anc.name.substring(0,4) == yy && anc.name.substring(4,6) == mm) continue;
else return document.anchors[--pointer].name;
}
return document.anchors[document.anchors.length-1].name;
}
function goToAnchor() { // adapted from Glenn G. Vergara
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;
var anchor_name = "#" + findAnchor(yy,mm,dd);
var url = (location.href.indexOf('#') != -1) ?
location.href.substring(0,location.href.indexOf('#')) : location.href;
location.replace(url + anchor_name);
}
window.onload = goToAnchor;
</script>
</head>
<body>
<a name="20020831">August 31, 2002<hr /><hr /><hr /><hr /><hr /><hr /><hr /><hr />
<a name="20020904">Sept 4, 2002<hr /><hr /><hr /><hr /><hr /><hr /><hr /><hr />
<a name="20020909">Sept 9, 2002<hr /><hr /><hr /><hr /><hr /><hr /><hr /><hr />
<a name="20020911">Sept 11, 2002<hr /><hr /><hr /><hr /><hr /><hr /><hr /><hr />
<a name="20020916">Sept 16, 2002<hr /><hr /><hr /><hr /><hr /><hr /><hr /><hr />
<a name="20020924">Sept 24, 2002<hr /><hr /><hr /><hr /><hr /><hr /><hr /><hr />
<a name="20031003">Oct 3, 2003<hr /><hr /><hr /><hr /><hr /><hr /><hr /><hr />
<hr /><hr /><hr /><hr /><hr /><hr /><hr /><hr /><hr /><hr /><hr /><hr /><hr />
<hr /><hr /><hr /><hr /><hr /><hr /><hr /><hr /><hr /><hr /><hr /><hr /><hr />
</body>
</html>
Gordo
09-24-2002, 05:26 PM
Elegant!? I wouldn't know what that is!
Hey, if you or anyone feels the need to clean up the code, by all means have at it. But my test of this seems to work great! I won't be able to plug it into my web page until I get home. If I have any problems, I'll let you know....but I don't anticipate any problems.
THANKS AGAIN!!!
:thumbsup: :thumbsup: :thumbsup:
<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
}
onload = goToAnchor
</script>( •) (• )
>>V
adios
09-25-2002, 12:45 AM
Now, that's elegant...
owl I hate you ;)
Gordo
09-25-2002, 02:08 AM
Owl definitely got you on that one adios. Ahh, but it's all in the creative spirit. And better yet, we've all learned something...or at least I have. Thanks again!
Originally posted by adios
owl I hate you ;) I apologize for the script, adios, normally I refrain from showing
off like that, but I had to get your attention somehow.
Something bugs me,
are you sure we haven't met before on
another forum, you with a different name?
There's something in your style, knowledge, fluent and thorough
explanations that reminds me this great guy who was a citizen of
the forum I regularly answer in. I was really sad when he left.
So?
( •) (• )
>>V
whammy
09-26-2002, 12:56 AM
I dunno... but that was simplicity at its finest. I think we can all appreciate that. :D
I know i can. :)
adios
09-26-2002, 02:10 AM
Nope.
Last time I talk to an A.A. Milne character.
owlman - only being envious. You remind me of someone I once knew who was a champion thread-ender, unassumingly tossing in his 2p, generally worth £1,000,000. If you're in the neighborhood, get my attention anytime.
We learn wisdom from failure much more than from success. We often discover what will do by finding out what will not do; and probably he who never made a mistake never made a discovery. Samuel Smiles (1816-1904) :)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.