Activating a link

S

Spartanicus

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.

Am I going about this in an appropriate manner? If so I could use a
pointer to the JS feature that I need to activate an HTML link.

(As is probably obvious I know very little about JS)
 
R

Richard Cornford

Spartanicus said:
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.
 
S

Spartanicus

Richard Cornford said:
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.

[snip]

I managed to get it working using your information.
Difficult to say without the mechanics of the 'why?'.

This is what the code was for:
http://homepage.ntlworld.ie/spartanicus/temp/

At this stage it is only a coding experiment which has some use for
myself hosted locally. It is not currently suitable for internet
deployment, nor does it offer much functionality likely to be useful to
others. I may later try to make it suitable and more useful for internet
deployment, the sub linking feature was the first issue towards that aim
that I wanted to tackle.
 
I

Info_Junkie

Spartanicus said:
Richard Cornford said:
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.

[snip]

I managed to get it working using your information.
Difficult to say without the mechanics of the 'why?'.

This is what the code was for:
http://homepage.ntlworld.ie/spartanicus/temp/

At this stage it is only a coding experiment which has some use for
myself hosted locally. It is not currently suitable for internet
deployment, nor does it offer much functionality likely to be useful to
others. I may later try to make it suitable and more useful for internet
deployment, the sub linking feature was the first issue towards that aim
that I wanted to tackle.

I really liked the page in the "example" link. I would think that the
site could be very popular to people interested in the city of Dublin
and for navigation thereof. Well designed and good navigation
techniques.

Info_Junkie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top