window.open() in an iPad on load of a frame does n

2019-02-23 18:11发布

I am trying to modify a site that uses "Morten's JavaScript Tree Menu" to display PDFs in a frames set using the Adobe Reader plug-in.

On the iPad the frame is useless, so I want to open the PDF in a new tab. Not wanting to mess with the tree menu I thought I could use JavaScript in the web page being opened in the viewer frame to open a new tab with the PDF.

I am using window.open() in $(document).ready(function() to open the pdf in the new tab. The problem is that window.open() does not want to work in the iPad.

The body of the HTML normally looks like this...

<body>
    <object data="MypdfFileName.pdf#toolbar=1&amp;navpanes=1&amp;scrollbar=0&amp;page=1&amp;view=FitH" 
    type="application/pdf" 
    width="100%" 
    height="100%">
    </object>
</body>

I changed it to only have a div like this...

<body>
    <div class="myviewer" ></div>
</body>

Then used the following script...

$(document).ready(function() {
    var isMobile = {
        Android : function() {
            return navigator.userAgent.match(/Android/i) ? true : false;
        },
        BlackBerry : function() {
            return navigator.userAgent.match(/BlackBerry/i) ? true : false;
        },
        iOS : function() {
            return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true : false;
        },
        Windows : function() {
            return navigator.userAgent.match(/IEMobile/i) ? true : false;
        },
        any : function() {
            return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows());
        }
    };

    if(isMobile.any()) {
        var file = "MypdfFileName.pdf";
            window.open(file);
    }else {
        var markup = "<object   data='MypdfFileName.pdf#toolbar=1&amp;navpanes=1&amp;scrollbar=0&amp;page=1&amp;view=FitH' type='application/pdf' width='100%' height='100%'></object>";
        $('.myviewer').append(markup);
    };
});

Everthing works except for window.open() on the iPad. If I switch things around widow.open() works fine on a computer. In another project I am using window.open() successfully on the iPad from an onclick function.

I tried using a timer function. I also tried adding an onclick function to the div and posting a click event. In both cases they worked on a computer but not an iPad. I am stumped.

I know it would make more sense to handle the ipad in the tree menu frame, but that code is so complex I can't figure out where to put/modify the onclick event.

Is there a way to change the object so that it opens in a new tab?

Is anyone familiar enough with Mortens Tree Menu code that can tell me how to channge the on click event so that it opens the pdf in a new tab instead of opening a page in the frame?

Thanks

2条回答
ら.Afraid
2楼-- · 2019-02-23 18:37

You should also check your element and parent elements and make sure you do not have a ":hover" pseudo class on there. When you touch that element expecting a "click" event to occur, it will trigger the hover state on the element. If you touch again it will then fire the click event.

查看更多
手持菜刀,她持情操
3楼-- · 2019-02-23 18:41

probably Safari tries to block popups. That means that when the user clicks, does an action, window.open works, but not when it "happens".

查看更多
登录 后发表回答