0

I have an odd situation, where some of the 6000 users of an online system are getting incomplete documents printed out.

I have the following code at the bottom of the web page.

<script type="text/javascript" defer="defer">
  window.onload = function(){@Html.Raw(ViewBag.StartupScript)};
</script>

and ViewBag.StartupScript is simply window.print();

Most of the users get the full document with all the images loaded. But a handful get all the text, but one or more) of the images are missing.

The images are at the bottom of the document. The images are on the same webserver (no cdn). We have to use IE or Edge for other legacy reasons. I cannot duplicate this at my workstation, I always see all the images.

The only thing I can think of is that in some cases, the network, or individual PC is slow, and so is not receiving the whole document to print it, despite the document.ready function. However, from what I understand, window.onload only fires when all the images have been loaded.

So, to keep this to questions not a discussion. Have I somehow misunderstood window.onload (and defer="defer", and the way they are used?

Is there an alternative way I can guarantee that the document and all its images are downloaded before it is printed.

And is there an easy way of proving my working theory that the print command is executed before the images are loaded?

Just to muddy the waters, it appears that the PC's this does not work on are mainly Windows 7, not windows 10 running IE. I haven't proved this definitely yet, but it may be issue in the OS / browser implementation.

To further update this (it still doesn't work properly). Some images are being aborted by ie, yet there is no consistency. Sometimes it is the first image that is aborted, other times on that page, the same image is loaded and a separate one is aborted. ((abort) in network tools. Googling this issue implies it is a mime type issue. but then I would expect it to reject or accept all mages, not act sporadically. Anyone got anymore ideas, if they are even still reading this?

Matt
  • 1,596
  • 2
  • 18
  • 32
  • try moving that script to the bottom of the markup... place it before `

    ` closing of body tag

    – Muthu Kumaran Sep 26 '17 at 11:13
  • 1
    Can u replace window.onload = function(){@Html.Raw(ViewBag.StartupScript)}; with $(document).ready(function(){@Html.Raw(ViewBag.StartupScript)}); – Ayaz Sep 26 '17 at 11:15
  • @MuthuKumaran the code is at the bottom. – Matt Sep 26 '17 at 11:32
  • $(document).ready doesn't work as that fires when the DOM is loaded, not the page rendered, so it makes no difference. Thanks though – Matt Sep 26 '17 at 12:19
  • If you feel like you're trying to do everything right, and it still isn't working, could you inject a small extra bit of a delay? – Greg Sep 26 '17 at 14:26
  • On Defer, it appears it's meant to defer loading of the external script file itself. That it may be ignorable for inline scripts. See: https://www.w3schools.com/Tags/att_script_defer.asp "Note: The defer attribute is only for external scripts (should only be used if the src attribute is present)." – Greg Sep 26 '17 at 14:27
  • Otherwise, it does appear you have the right idea about window.onload, as per https://stackoverflow.com/questions/588040/window-onload-vs-document-onload – Greg Sep 26 '17 at 14:30

1 Answers1

0

Make sure the content is fully loaded by setting an eventlistener.

$(window).on("load", function() {

});

If still doesn't solve the problem. Try below. Check if images are loaded before calling the window.print method.

$('#imageId').load(
  function () {

});
mausinc
  • 527
  • 4
  • 14
  • This has made no difference either. – Matt Sep 26 '17 at 12:39
  • Are all the images fixed? – mausinc Sep 26 '17 at 13:51
  • They are dynamically added depending on the logged in user, and the customer the paperwork relates to if that's what you mean? Each of the 3 images are in a line, and each is wrapped in a
    ...
    tag.
    – Matt Sep 26 '17 at 14:23
  • yes, can you give the 3 images unique identifiers and check if they are loaded the way i showed above? – mausinc Sep 26 '17 at 14:41
  • Will do (Probably tomorrow now). Am just about to update the original question as we have figured out some more information – Matt Sep 26 '17 at 15:10