Send information about the link clicked to the server before redirecting

Asked

Viewed 78 times

0

How to fire an AJAX event when the user clicks on a link? Whereas, the link will redirect him to another page.

I tried to carry out the process, however, sometimes the event is not triggered(I believe it gives no time function to be triggered because of redirection).

<a id="clique" href="https://www.google.com">Clique Aqui</a>

<script>
$(document).on("click", "a#clique"", function(){

$.ajax({

/* .... */

});

});
</script>

My intention with this is to record the user click.

  • 3

    Alexander, I didn’t vote for your question, but I think you have a very good reputation for asking a question of this quality. Try to improve it. For example, what you mean by "record click"?

  • Thanks @Sam for the thoughts. I asked the question very quickly, and I didn’t mind giving the question quality, I hope it’s clear now. It may still be a simple question, or be in duplicate, but I’m after that answer.

2 answers

1

Hello! Either I didn’t understand anything or what you are questioning yourself already solved (read my comment below to make it clearer!)

Run next. From what I see when opening the new window, before the browser executes the onclick function.

 
$(document).on("click", "a#clique", function(){
 console.log('teste, fazer alguma coisa! guardar informações importantes');
 alert( 'Clicked!' );
//$.ajax({ });

});
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a id="clique" href="/">Clique Aqui</a>

  • 1

    I did not notice his statement "I tried to carry out the process, however, sometimes the event is not triggered (I believe that there is no time it gives function to be triggered because of redirection)." As for that, I think the fact that you’re trying to run in AJAX in the background is causing the failure at times. Note that with Alert it gets stuck, so you can’t do the "click save" without using AJAX to test?

  • I understand that as an option, I could put e.preventDefault() in the element click, launch ajax, and then redirect with windows.location.href.. But I don’t trust the Javacript redirection (I believe some browsers may deny, or ask the user, because it is abstract), and also because I would have to wait for the request to be completed for later redirection by JS.

  • I don’t understand another way to save the click without using AJAX (in this case, forward this click data, to be saved later in the database), so I can use this data (such as clicks or views). I believe I would have to, on the landing page, create some rules and account for that click.

  • https://stackoverflow.com/questions/10295354/send-information-about-clicked-link-to-the-server-before-redirect?noredirect=1&lq=1

  • What if you go through a function that will load the address in question? vaiparaosite('https://google.com"); It would not be clearer, more practical and :/

1

You can modify href at click time and intercept the URL in a PHP file:

$(document).on("click", "a#clique", function(){
 $(this).attr('href', 'click_register.php?url=' + encodeURIComponent($(this).attr('href')));
 console.log( $(this).attr('href') );
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<a id="clique" href="https://www.google.com">Clique Aqui</a>

and the click_register.php it would be something like that:

<?php
    // salvar o click vindo na $_GET['url']
    // cuidando para não emitir nenhum retorno
    // em seguida redirecionar para a URL original 
    header("Location: " . $_GET['url']);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.