简体   繁体   中英

How to make a Href link wait

I want to make a Clickable button, which waits 10 seconds to load the linked page. I was wondering if it also needs Href for it ? If anyone know's how please help me out.

As you have tagged Javascript and not jQuery....

Something like

JavaScript:

function loadUrl(){
  window.location.href = "http://www.google.com";
}

Link:

<a href="JavaScript:setTimeout(loadUrl,10000)">Click My Link</a>

OR

JavaScript:

function delayUrlLoad(url, mils)
{
    setTimeout(function() {
        window.location.href = url;
    }, mils)
}

Link:

<a href="#" onclick="delayUrlLoad('http://www.google.com/', 10000);return false;"> Click Here </a>

How about?

HTML:

    <button id="yourbutton" href="https://www.google.com">
        Click Me
    </button>

jQuery/JS:

$( "#yourbutton" ).on( "click", function(event) {
  var url = $(this).attr('href');
  setTimeout("loadPage(url)", 10000);
  event.preventDefault();
});

function loadPage(url){
  window.location.href = url;
}

FIDDLE (it wont load the new page in JSFiddle as it is sandboxed, but if you check console, it is indeed attempting to load the page after the timeout).

You'll need to include jQuery to run this code, but this is the idea

<a href="http://www.url.com">Click Me</a>
$("a").on("click", function (event) {
  event.preventDefault();
  var timeout = setTimeout(function () {
    var url = $(this).attr("href");
    location.replace(url);
  }, 10000);
});

This way:

HTML

<span id="link" data-href="http://www.google.it">click here</span>

JS

$('#link').on('click', function() {
    setTimeout(function() {
        location.href = $('#link').attr('data-href');
    }, 10000);
});

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