setTimeout для всех onclick

bat

Former алкаш
Регистрация
24 Сен 2009
Сообщения
1.145
Реакции
445
Всем шалом!
Помогите запилить скрипт который будет находить на сайте все ссылки типа onclick и при клике по ним выставлять задержку перехода на 5 секунд ....
Код:
<script>
$(document).ready(function() {
$("a").each(function() {
var href = $(this).attr("href");
var target = $(this).attr("target");
var text = $(this).text();
$(this).click(function(event) { // when someone clicks these links
event.preventDefault(); // don't open the link yet
setTimeout(function() { // now wait 300 milliseconds...
window.open(href,(!target?"_self":target)); // ...and open the link as usual
},5000);
});
});
});
</script>
отлично выставляет задержку для всех ссылок типа a href, а нужно что то аналогичное для onclick
 
Код:
    <html>
    <head><title>test</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    </head>
    <body>
    <script>
    $(document).ready(function() {
    $("a").each(function() {
    var href = $(this).attr("href");
    var target = $(this).attr("target");
    var text = $(this).text();
    $(this).click(function(event) { // when someone clicks these links
    event.preventDefault(); // don't open the link yet
    setTimeout(function() { // now wait 300 milliseconds...
    window.open(href,(!target?"_self":target)); // ...and open the link as usual
    },5000);
    });
    });
   
   
    $("div").each(function() {
    var href = $("div").attr("onclick");
    $("div").removeAttr('onclick');
    var real_url = href.split("'");//('http://')
    var new_url = (real_url[1]);
   
    $("div").click(function(event) { // when someone clicks these links
    setTimeout(function (){
   
                 window.open(new_url,"_self");
   
             }, 5000);
     // ...and open the link as usual
   
    });
   
    });
   
    });
    </script>
    <a href ="http://google.com"> Google </a>
    <div onclick="location.href='http://yahoo.com'" >test</div>
   
    </body>
    </html>

Для просмотра ссылки Войди или Зарегистрируйся

Код:
    <html>
    <head><title>test</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    </head>
    <body>
    <script>
    $(document).ready(function() {
    $("a").each(function() {
    var href = $(this).attr("href");
    var target = $(this).attr("target");
    var text = $(this).text();
    $(this).click(function(event) { // when someone clicks these links
    event.preventDefault(); // don't open the link yet
    setTimeout(function() { // now wait 300 milliseconds...
    window.open(href,(!target?"_self":target)); // ...and open the link as usual
    },5000);
    });
    });
    
    
    jQuery('div').each(function() {
        $(this).data('onClick', this.onclick);
        $(this).attr('onClick', '');
        $(this).click(function(event) {
            window.setTimeout( function(obj) {
                return function() {$(obj).data('onClick').call(obj, event || window.event)};
            }(this), 5000)
        });
    });
    
    });
    </script>
    <a href ="http://google.com"> Google </a>
    <div onclick="location.href='http://yahoo.com'" >test</div>
    
    </body>
    </html>

Для просмотра ссылки Войди или Зарегистрируйся
 
Последнее редактирование:
Назад
Сверху