我不能使用target=_blank,因为我的操作是动态构建的,例如:当用户点击链接时,我用.click(函数(事件))捕获它,然后调用ajax api,然后在回调中我有了要去的url。
现在的问题是:
window.location=mylink;//工作-好!window.open(mylink);//不起作用-不好!
为什么?
我需要打开一个新窗口,因为mylink是一个指向pdf文件的链接,当使用window.location时,pdf文件会被正确查看,但是,。。而不需要浏览器的任何反向导航控件。这是一个移动网络应用程序,我使用jquery mobile。
糟糕的是,在互联网上,许多其他人都有pdf查看器的问题,但没有人解决它,所以我想打开一个新窗口,将pdf链接传递到那里。这样我父母的窗户就不会动了。否则,您必须终止当前会话并再次打开safari。。
将target="_blank"添加到您的链接中。然后,在单击事件时调用此脚本。它将在新窗口中打开您的pdf。
$( "a" ).live( "click", function(event) {
var $this = $(this),
//get href, remove same-domain protocol and host
href = $this.attr( "href" ).replace( location.protocol + "//" + location.host, ""),
//if target attr is specified, it's external, and we mimic _blank... for now
target = $this.is( "[target]" ),
//if it still starts with a protocol, it's external, or could be :mailto, etc
external = target || /^(:?w+:)/.test( href ) || $this.is( "[rel=external]" ),
target = $this.is( "[target]" );
if( href === '#' ){
//for links created purely for interaction - ignore
return false;
}
var testtarget = $this.attr( "target" );
if (testtarget == '_blank') {
alert('Leaving web app');
return true;
}
$activeClickedLink = $this.closest( ".ui-btn" ).addClass( $.mobile.activeBtnClass );
if( external || !$.mobile.ajaxLinksEnabled ){
//remove active link class if external
removeActiveLinkClass(true);
//deliberately redirect, in case click was triggered
if( target ){
window.open(href);
//return true;
}
else{
location.href = href;
}
}
else {
//use ajax
var transition = $this.data( "transition" ),
back = $this.data( "back" ),
changeHashOnSuccess = !$this.is( "[data-rel="+ $.mobile.nonHistorySelectors +"]" );
nextPageRole = $this.attr( "data-rel" );
//if it's a relative href, prefix href with base url
if( href.indexOf('/') && href.indexOf('#') !== 0 ){
href = getBaseURL() + href;
}
href.replace(/^#/,'');
changePage(href, transition, back, changeHashOnSuccess);
}
event.preventDefault();
});
如果要在jQuery Mobile中打开对话框/弹出窗口,则必须使用属性data-rel="dialog"
,并且href
属性必须指向要加载的URL
<a href="yourpage.html" data-role="button" data-rel="dialog">Open dialog</a>