Home > browser compatibility, javascript, pdf > Fix for initial opening pdf in new Internet Explorer (6-8) window problem

Fix for initial opening pdf in new Internet Explorer (6-8) window problem

Problem:
In a recent project I am working on I need to allow user to open their documents (in PDF format, hosted in a different server with different domain) in a new window. This seems to be a pretty straight-forward task as all I need is window.open (url is not hard-coded in the href as user need to get authenticated with the local server and get the token that will be used to access the document) but when it comes to Internet Explorer I got stuck with a weird problem. For versions 6-8, the first click on the pdf view link since the initial launch of IE browser always results in a blank window. User needs to either press F5 on the popped-up window click the view link (same or different) again get PDF loaded. The following code works without issues in all browsers except IE6-8:

HTML:

<a href="void(0)" onclick="javascript:opendoc('abc123')">View Document</a>

JS:

function opendoc(docid) {
    // skipping code getting token via ajax call
    var url="https://remote.host.url/somepath&docid="+docid+"&token="+token;
    var win=window.open( url, 'newwindow' );
}

Through a lot of trial and errors which include the followings:

  • refresh the new popup after some time
  • close the popup then open it again after some delay
  • server-side redirect
  • pdfobject
  • using embed tag

I found a workaround that actually works. Basically the same url got opened twice (only for the initial loading after launching browser) with 5 seconds apart if client browser is IE6-8. Note: The last two methods fail as well. I think it has something to do with the document.domain setting in the parent window.

Solution:

// this variable is to store if the double loading trick has been done
var first_clicked=0;
function opendoc(docid) {
    // skipping code getting token via ajax call
    var url="https://remote.host.url/somepath?docid="+docid+"&token="+token;
    var win=window.open( url, 'newwindow' );
    // using jQuery to do browser testing, feel free to use other methods
    if(!first_clicked && $.browser.msie && $.browser.version.substr(0,1)<'9') {
        setTimeout( function() {
            win=window.open( url, 'newwindow' );
        }, 5000 );
        // adjust the time to be shorter but be sure to
        // do enough tests as I found if the value is too low (300, 500, 1000 for example) it won't work either
        // it may have something to do with the connection speed to remote server
        
        first_clicked=1; // set to 1 so sequential clicks open the link only once
    }
}

This doesn’t seem to be a perfect fix as it still requires user to wait for a few seconds before the first result can be viewed but it’s better than leaving user in the dark or asking them to press F5 in order fix the problem.

Advertisements
  1. sdca1988
    February 16, 2012 at 7:23 pm

    I have same problem, I download the latest Adobe Reader the problem went away.

    • ricoch3n
      February 16, 2012 at 11:50 pm

      Yes this would be another solution to this problem but it relies on users themselves to figure it out. This won’t work if:
      1) user doesnot know it’s adobe reader’s problem
      2) user doesn’t have privilege to update software, adobe reader in this case.

      Thanks for sharing nevertheless.

  2. May 27, 2012 at 6:45 am

    I was suggested this website through my cousin. I’m now not certain whether or not this submit is written through him as no one else know such exact approximately my problem. You’re wonderful! Thank you!

    • ricoch3n
      May 31, 2012 at 10:27 pm

      Glad to know that this workaround helps.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: