Skip to content Skip to sidebar Skip to footer

How To Create A Javascript That Clicks A Particular Url After A Certain Amount Of Delay?

I would like to create a JavaScript that clicks a particular LINK after a certain amount of delay say e.g. 10 seconds on an HTML page. Can you please provide me with the JavaScript

Solution 1:

Include this method (however you want):

functionfireEvent(element,event) { 
   if (document.createEvent) { 
       // dispatch for firefox + others var evt = document.createEvent("HTMLEvents"); 
       evt.initEvent(event, true, true ); // event type,bubbling,cancelable return !element.dispatchEvent(evt); 
   } else { 
       // dispatch for IE var evt = document.createEventObject(); 
       return element.fireEvent('on'+event,evt) 
   } 
} 

Then call it:

window.setTimeout(function() { 
    var e = document.getElementById('yourLinkId');
    if(e) fireEvent(e, 'click');

}, 10000); 

Solution 2:

window.setTimeout(function() {
    window.location = "http://your_url_here.com";
}, 10000);

The second parameter is the time in milliseconds. So 10000 milliseconds is 10 seconds.

Solution 3:

If you need the delay something like the one shown on this site (See the "Testimonials" container at the bottom-left corner of the page), use the following jQuery code.

(function($){
$(document).ready(function(){
    var el = $("#testimonial"); //The id of the container. It may be a `<div>`if (el){
    RotateTestimonial();
    setInterval(RotateTestimonial, 20000);
    }
});

functionRotateTestimonial(){
    var pageUrl = "RandomTestimonial.jsp"//The page where the request goes.
    $.ajax({
            type: "GET",
            url: pageUrl,
            cache:false,
            success: function(msg) {                   
                $("#testimonial").slideUp('slow').fadeOut(3000, function (){
                    var el = $("#testimonial"); 
                    el.html(msg);
                    el.slideDown('slow').fadeIn('slow');
                });
            }
    });
}
})(jQuery)

Post a Comment for "How To Create A Javascript That Clicks A Particular Url After A Certain Amount Of Delay?"