Robert Atkinson wrote:
I have hyperlink, <a class='navbar'
href="http://my.calendars.net/colchestercamra/">Diary</a>, that
points to a calendar.
What I want to be able to do is jump to
calendars.net/colchestercamra/#month, where I use JavaScript to set
up the month.
Could I use an 'onclick' to set up the properties of the <a> tag just
before it jumps? I can't use window.location, as this currently
remains static (using frames I'm afraid!).
A - elements have two properties that can be used by an - onclick -
handler to set a fragment identifier on the URL that they are about to
navigate to' the - href - property and the - hash - property. And -
onclick - handler is executed as a mehtod of the A element so the -
this - keyword is a reference to that element. As a result a fragment
identifier may be appended to the URL in the - href - property with:-
this.href += '#month';
- or assigned to the - hash - property as:-
this.hash = '#month';
Using the - href - is only safe if you know that it will never have a
fragment identifier already. Using the - hash -property will replace any
existing (default?) fragment identifier.
So, with:-
var mnths = ['January', 'February', 'March', 'April','May','June',
'July', 'August', 'September', 'October','November',
'December'];
- defined globally, the A element:-
<a href="http://example.com/page.html"
onclick="this.hash = '#'+mnths[(new Date()).getMonth()];">
.... </a>
- would navigate to:-
http://example.com/page.html#July
- this month, and:-
http://example.com/page.html#August
- next month (at least when javascript is enabled in the browser.
Otherwise it would just use the raw - href - attribute (With any default
fragment identifier it may have.)).
The - onclick - handler must either not have a return value, or return -
true - as it does not want to cancel the navigation, having just
modified the URL that will be navigate to.
Richard.