Skip navigation.

Prepare!

Lee Harvey's Zombie Hit Parade

Opening file types without opening new page

, , , ,

For those of you who hate to see a "Blank page" appear when clicking links for certain file types, I present the following User JavaScript file...

Download/view pdfHandler.js
document.addEventListener("load",
  function(e) {
    if (!document.body) document.body = document.getElementsByTagName("body")[0];
    if (!document.body) return;
    if (!document.links) return;
    for (var i = 0; i < document.links.length; i++) {
      var href = document.links[ i ].getAttribute("href");
      if (!href) continue;
      if (!href.match(/\.pdf$/) &&
          !href.match(/\.doc$/) &&
          !href.match(/\.xls$/))
          continue;

      if (!document.blankIframeHandler) {
        document.blankIframeHandler = document.createElement("iframe");
        with (document.blankIframeHandler) {
          setAttribute("src", "about:blank");
          setAttribute("id", "blankIframeHandler");
          setAttribute("name", "blankIframeHandler");
          setAttribute("style", "display:none;visibility:hidden;");
        }
        document.body.appendChild(document.blankIframeHandler);
      }
      document.links[ i ].setAttribute("target", "blankIframeHandler");
    }
  }, false);


This User JavaScript creates a hidden inline frame (iframe element) on the current page, then targets all .pdf, .doc, and .xls links to open inside that hidden inline frame.

This technique seems fairly effective at opening file types with associated programs in Opera, such as .pdf files for Adobe Reader.

Enjoy.

My new addition to the familyA simple cross-browser technique to block potentially harmful websites

Comments

mav1976 26. March 2006, 11:22

Hi Lee,

thank you for this script. :up:

mliebelt 4. May 2006, 09:34

Hi Lee,

do you have an idea how to adapt that scheme to hide the emtpy page when selecting an attachment in a mail? Currently, I get the empty page. When I test it with an HTML file with embedded link, your solution works as expected.

Bye Markus

wunage 2. October 2007, 16:14

Good job. :happy:

dapxin 2. April 2009, 04:33

thx

Write a comment

You must be logged in to write a comment. If you're not a registered member, please sign up.