Spartanicus wrote:
I have a list of links using the following construct:
<a href='#artane' onclick='pos(1107,281)'>Artane</a>
The links offer additional functionality through JS.
There are no actual fragment IDs on the page, but the
fragment coding allow the links to be bookmarked. Via
the window.onload event I check if a fragment ID is
present in the address, if so I need to activate the
'pos' function with the appropriate arguments.
I'd like to loop through the links, if one has a href
attribute value that matches the window.location.hash
value I'd like to activate that link so as to avoid
having to duplicate the 'pos' function's arguments
within the JS itself.
There is traditional (and W3C formaised) - document.links - collection
that includes all the links in a document. That could be iterated through
checking the - href - properties of the objects found. Those - href -
properties should be expected (but not required) to be normalised to
fully qualified URLs, so use - indexOf - (or similar) instead of straight
comparison.
The objects in the links collection may have (and will have is the HTML
attribute exists) an - onclick - property that is a reference to the
function that the browser generates from the HTML attribute. You may call
that function as a method of the object, in exactly the same way as the
browser would in response to an event. And as you are not using the event
object in the attribute code you will not even have to worry about faking
an event object.
For example, the onclick handler for the fist link on a page may be
called as:-
document.links[0].onclick();
This will indirectly execute your - pos - function with the arguments
used in the HTML attribute's call. However, it will _not_ navigate the
document, but as the fragment identifier is apparently already on the URL
at this point not having it added by navigation should make no
difference.
On the other hand if you wanted to call the - onclick - handler and then
navigate the document to the fragment identifier you could write a
modified URL to the - location - object, or assign the fragment
identifier to - location.hash -.
Am I going about this in an appropriate manner?
<snip>
Difficult to say without the mechanics of the 'why?'.
Richard.