SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict crabby80's Avatar
    Join Date
    May 2007
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trigger an href event only after the onclick

    Hi guys

    Is there a way to trigger an href event only after the onclick event has finished?

    IE <a onclick="track()" href="page.html">link</a>

    I need the track function to have finished before the link goes to page.html

    Any help would be much appreciated

    Crabby

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's how it works the way you have it.

    Try this:
    <a href="page.html" onclick="alert('test');">click me</a>

    Notice that page.html doesn't try to load till you've clicked the ok button of the alert box.

  3. #3
    SitePoint Member Aedus's Avatar
    Join Date
    Nov 2007
    Location
    Abu Dhabi, UAE
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Make sure track() doesn't return a value that can be equated to false, or the link will not be followed.

    But like jimfrazier said, the events trigger will execute and complete before the browsers default href event is followed.

  4. #4
    SitePoint Addict crabby80's Avatar
    Join Date
    May 2007
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi guys

    Perfect thanks guys, I realised what I was doing wrong, I wasn't returning true from my track function, DOH!!

    PHP Code:
    function track(type,title) {
        
        
    //alert("track");
                
        
    var url 'ajax_controllers/tracking.php?type=' type '&title=' title;
        
        new 
    Ajax.Request(url, {
                            
    method:'GET',
                            
    onSuccess: function(transport) {
                                
    //alert(transport.responseText);
                                
    return true;
                            }
                        });

    Seems to have solved it

  5. #5
    SitePoint Addict crabby80's Avatar
    Join Date
    May 2007
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah, think I spoke to soon

    my track function records to the db using ajax, and sometimes I notice the values have not been added especially with links that open light windows?

    Hmmmmmm

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, well, AJAX is asynchronous (hence the first A) - so once you click the button the function returns and another thread goes off and does the http work, while the rest of the function continues. So you can't rely on the function completing before the next page loads.

    Maybe what you should do is something like:
    Code:
    <a onclick="track('theType, 'theTitle, this.href);return false;" href="page.html">link</a>
    
    ...
    
    function track(type, title, nextPage) {
    	var url = 'ajax_controllers/tracking.php?type=' + type + '&title=' + title;
    
    	new Ajax.Request(url, {
    
    		method:'GET',
    
    		onSuccess: function(transport) {
    
        	//alert(transport.responseText);
    			location.href = nextPage;
    			return true;
    		}
    
    	});
    
    }
    I will comment that it seems odd to use AJAX in this instance, instead of simply passing the parameters to php via querystring or form when the user clicks the link. Consider:

    the ajax way: user clicks a link -> http request sent to server for tracking, then another http request sent to server to load next page

    the POST/GET way: user clicks a link -> http request sent to server, php handles the tracking and serving up the new page

  7. #7
    SitePoint Addict crabby80's Avatar
    Join Date
    May 2007
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey jimfraser

    Thanks alot for your reply, I had a feeling a solution such as the one you provided would be the way around it.

    Interesting point, that would be a much better way of doing it

    Thanks again


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •