简体   繁体   中英

Calling javascript function and also hitting a link in same anchor tag

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

I need to save cookies before i navigate to new page.

a href=/dashboard/sample.html onclick="savecookies()"

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
});

Yes you can do it via JQuery. You can set any anchor link with a 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 "unprevent" and let the href without "#" simbol but i prefer this way IMHO it's better if you handle it.

创建一个函数并粘贴用于保存cookie的代码,然后在函数末尾粘贴用于导航的代码。

you dont need to redirect through JS code, just grab the link and bind a click function to it which executes your function and then returning true;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM