【问题标题】:window.open() does nothing in jquery mobile?window.open() 在 jquery mobile 中什么都不做?
【发布时间】:2012-03-23 11:01:11
【问题描述】:

我不能使用 target=_blank 因为我的操作是动态构建的,例如:当用户点击链接时,我用 .click(function(event) 捕获它,然后调用 ajax api 然后在回调中我有去哪里的网址。

现在的问题:

window.location = mylink; // 工作 - 好的! window.open(mylink); // 不工作 - 不行!

为什么?

我需要打开一个新窗口,因为 mylink 是指向 pdf 文件的链接,并且在使用 window.location 时可以正确查看 pdf 文件,但是,..没有浏览器的任何后退导航控件。这是一个移动网络应用程序,我使用 jquery mobile。

在互联网上太糟糕了,许多其他人都有 pdf 查看器的问题,但没有人解决它,所以我想打开一个新窗口并在那里传递 pdf 链接。这样我的父窗口将保持不变。否则,您必须终止当前会话并再次打开 safari..

【问题讨论】:

    标签: jquery jquery-mobile window.open


    【解决方案1】:

    将 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();
    });
    

    【讨论】:

      【解决方案2】:

      如果你想在 jQuery Mobile 中打开一个对话框/弹出窗口,你必须使用属性 data-rel="dialog" 并且 href 属性必须指向你要加载的 URL

      <a href="yourpage.html" data-role="button" data-rel="dialog">Open dialog</a>
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-02
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 2014-04-16
      • 1970-01-01
      相关资源
      最近更新 更多