Slowdown https: //stackoverflow.com/speedshttps: //stackoverflow.com/executionhttps: //stackoverflow.com/

I have several on my website href's, and I need to add a delay between when they are clicked and when they load. Since there are hundreds hrefs, I cannot have a separate js function for each of them.

In two ways that I explored, the contents of href were passed to javascript as a variable, for example:

<a href="example1.php">example1</a>
<a href="example2.php">example2</a>
<a href="example3.php">example3</a>
<script>
var href = ### CONTENTS OF SELECTED HREF ###
$(function() {
  $("a").on("click", function(event) {
    event.preventDefault();
    setTimeout(function(){window.location.href = href;}, 1000);
  });        
});

Can this be done using CSS?

+4
source share
1 answer

No, this cannot be done using CSS (cascading style sheets, in no way). You will need to use Javascript.

, a, href .

$(function() {
  $("a").on("click", function(event) {
    event.preventDefault();
    var href = this.href;
    setTimeout(function(){
        window.location = href;
    }, 1000);
  });        
});
+6

Source: https://habr.com/ru/post/1622853/


All Articles