user2837961
user2837961

Reputation: 1555

IE 11 onload is never called

My web page has following javascript

function LoadPDF(filename)
{
    var loc = filename;
    document.getElementById("pdf").setAttribute("src", loc);
    if (window.addEventListener) {
        document.getElementById("pdf").addEventListener("load", LoadPrint, false);
    }
    else if (window.attachEvent) {
        document.getElementById("pdf").attachEvent("onload", LoadPrint);
    }
    else {
        document.getElementById("pdf").onload = LoadPrint;
    }
}

function LoadPrint() {
    alert('fired!');
    if (document.getElementById("pdf").src !== "") {
       var frm = document.getElementById("pdf");        
       frm.contentDocument.getElementById("pdf").contentWindow.print();
    }
}

The LoadPDF is called from code behind. "pdf" is my iframe. When the pdf is loaded into the iframe I want to call LoadPrint. But the trouble is in IE 11 its never called.

Can anyone please help?

Upvotes: 0

Views: 3162

Answers (2)

Christos Lytras
Christos Lytras

Reputation: 37337

This is an IE11 bug, which MS refuses to fix because they consider it a feature bug and they no longer fix that kind of bugs for old browser versions.

A workaround to this bug, is to load the pdf file inside an other page iframe and then load that page inside your iframe. A simple javascript pdf loader with a file argument:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>PDF Loader</title>
<style type="text/css">
html, body {
    border:0;
    margin:0;
    height:100%;
    overflow-y:hidden;
}

#pdf {
    border:0;
    margin:0;
    width:100%;
    height:100%;
}
</style>
</head>
<body>

<iframe id="pdf"></iframe>

<script type="text/javascript">

function getParameterByName(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

var pdf = getParameterByName('pdf');
document.getElementById('pdf').setAttribute('src', pdf);

</script>
</body>
</html>

You can use it with the <filename.html>?pdf=<pdf_file_to_load>.

Then you just change your code to load the pdf file through that loader like this:

function LoadPDF(filename)
{
    var loc = "pdf-loader.html?pdf="+filename;
    document.getElementById("pdf").setAttribute("src", loc);
    if (window.addEventListener) {
        document.getElementById("pdf").addEventListener("load", LoadPrint, false);
    }
    else if (window.attachEvent) {
        document.getElementById("pdf").attachEvent("onload", LoadPrint);
    }
    else {
        document.getElementById("pdf").onload = LoadPrint;
    }
}

function LoadPrint() {
    alert('fired!');
}

LoadPDF('http://www.pdf995.com/samples/pdf.pdf');

Now the LoadPrint function is called on iframe load event even on IE11. Here is my working example you can even test with IE11: http://zikro.gr/dbg/html/ie11-iframe-pdf/

Here you can see a screen capture with the 10MB PDF loading and only after it finish loading it fires the load event and alerts the message:

enter image description here

Upvotes: 1

dgeare
dgeare

Reputation: 2658

I don't know if this is your specific issue in this instance, but make sure you bind your listeners to the element before you assign it's src attribute. If the item is in cache it is possible for the load event to fire before you bind to it, thus missing it entirely.

Upvotes: 0

Related Questions