Calling javascript function, as well as getting the link into the same anchor tag

Is it possible to call javascript funciton (for example, save cookie () here) before redirecting to a new page (sample.html in my example).

I need to save a cookie before moving to a new page.

a href=/dashboard/sample.html onclick="savecookies()" 
+4
source share
4 answers

use something like this:

 $('a').click(function() { saveCookie(); // save cookies document.location.href = "sample.html"; // forward user to new page return false; // ignore default src of anchor }); 
+3
source

Yes, you can do it through jQuery. You can set any binding with the class

 <a href="#someUrl" class="navigator (other class)" >MyAwesomeLink</a> 

Then in jQuery

  $(".navigator").click(function(e){ var url = $(this).attr('href').replace('#',''); /** Save your cookie stuff here **/ window.location.href = "PATH"+url; e.preventDefault(); } 

You can even "unlock" and enable href without a "#" simbol, but I prefer this method IMHO, it's better if you handle it.

+3
source

Make one function and paste the code to save cookies, and then at the end of the function, paste the code for navigation.

0
source

you don’t need to redirect via JS-code, just take the link and bind the click function to it, which executes your function, and then returns true;

 $(function(){ $('a').click(function() { //alert('clicked'); saveCookie(); return true; }); }) 
0
source

All Articles