当用户点击指向外部网站的链接时,如何显示模式弹出窗口几秒钟(表示它已重定向到外部网站),然后以新的方式打开链接窗口。
我已经在使用JQuery UI对话框了。最好,我不想修改HTML(即按照JQuery UI对话框的要求创建对话框),但想要纯粹的JQuery / JS解决方案,它将创建/注入对话框 并显示它。
到目前为止我的尝试:
$('a[rel=external]').click(
function(event)
{
event.preventDefault();
$('#redirectDialog').dialog('open').delay(2000);
$('#redirectDialog').dialog('close');
window.open(this.href);
return false;
}
);
不幸的是,对话框没有显示 - 它会立即在新窗口中打开链接。
任何帮助显示对话框,短暂停顿,然后关闭然后打开链接,并帮助动态创建对话将非常感激。
答案 0 :(得分:2)
为什么不使用显式超时?像这样:
$('a[rel=external]').click(function(event) {
event.preventDefault();
$('#redirectDialog').dialog('open');
var href = this.href;
setTimeout(function() {
$('#redirectDialog').dialog('close');
window.open(href);
}, 2000);
return false;
});
您使用setTimeout
来延迟,然后在window.open
回调中执行setTimeout
。
答案 1 :(得分:1)
你不应该使用window.open()。而是使用Window.location.href = URL;它将在同一窗口中加载该URL
$('a[rel=external]').click(
function(event)
{
event.preventDefault();
$('#redirectDialog').dialog('open').delay(2000);
$('#redirectDialog').dialog('close');
window.location.href = this.href;
return false;
}
);