Connecting Tech Pros Worldwide Help | Site Map

Follow link, then execute a function

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 17th, 2006, 03:55 PM
Spartanicus
Guest
 
Posts: n/a
Default Follow link, then execute a function

I need to execute a function after a link to a section within the same
document has been clicked and the navigation has occurred. When using
the onclick event handler the JS function is executed before the
navigation occurs. I can't tie it to a window.onload event since the
link only navigates within the already loaded document.

I'm not having any luck searching due to not knowing what keywords to
use. I looked at an explanation of bubbling vs capturing, but that
doesn't appear to be what I need.

--
Spartanicus

  #2  
Old November 17th, 2006, 03:55 PM
Daz
Guest
 
Posts: n/a
Default Re: Follow link, then execute a function


Spartanicus wrote:
Quote:
I need to execute a function after a link to a section within the same
document has been clicked and the navigation has occurred. When using
the onclick event handler the JS function is executed before the
navigation occurs. I can't tie it to a window.onload event since the
link only navigates within the already loaded document.
>
I'm not having any luck searching due to not knowing what keywords to
use. I looked at an explanation of bubbling vs capturing, but that
doesn't appear to be what I need.
>
--
Spartanicus
Enclose the function to be called within a function statement. For
example:

objectName.onclick = function(){ functionToBeCalled(); }

  #3  
Old November 17th, 2006, 04:15 PM
shimmyshack
Guest
 
Posts: n/a
Default Re: Follow link, then execute a function

so you mean moving to a named section using hash?

<a
name="1"
href="#2"

onclick="javascript:document.location='#2';alert(' youve_reached_2');"
Quote:
>
asdas
</a>

..
..
..
..
<a
id="2"
name="2"
href="#1"

onclick="javascript:document.location='#1';documen t.getElementById('1').onfocus
= function(){alert('youve_reached_1')};"
Quote:
>
asdas
</a>

the first onclick uses two js statements so you can be sure the first
executes before the second.
the second onclick does the same but uses onfocus which might be more
predictable.

dont use onclick though right, and this is a dirty solution!

  #4  
Old November 17th, 2006, 05:25 PM
VK
Guest
 
Posts: n/a
Default Re: Follow link, then execute a function


Spartanicus wrote:
Quote:
I need to execute a function after a link to a section within the same
document has been clicked and the navigation has occurred. When using
the onclick event handler the JS function is executed before the
navigation occurs.
Yes, this behavior is by design. As already suggested you can move the
navigation part to the script as well, just don't use the whole href
replacement on hash or IE before 6 may get dizzy or nasty of that :-)

Your links then could be like:

<a href="#anchor001" onclick="self.location.hash='anchor001';
myFunction(); return false">Anchor 001</a>

This way with script enabled "return false" will prevent the default
navigation but will execute the programmed one.

With script disabled you'll get just a regular anchor.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.