Click here to Skip to main content
13,833,874 members
Rate this:
Please Sign up or sign in to vote.
When downloading a pdf file, loading icon keeps on displaying in the page. It seems like beforeunload function is getting triggered when downloading a file. What would be the mistake in my code?

<script src="" type="text/javascript"></script>
        $(window).on('beforeunload', function(event) {
        $(window).on('load', function(event) {
 <div id="loading">Loading...</div>
 <a href="t1.php?pdf=<?php echo 'Intro.pdf';?>"><button>download</button></a>
            $bbpdf = $_GET['pdf'];
            header("Pragma: public"); // required
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Cache-Control: private", false); // required for certain browsers
            header("Content-Type: application/pdf");
            header("Content-Disposition: attachment; filename=\"" . basename($bbpdf));
            header("Content-Transfer-Encoding: binary");
            header("Content-Length: " . filesize($bbpdf));
Updated 14-Feb-18 16:16pm
ramyajaya 24-Mar-15 20:45pm
The problem I find in your code is $window.load is not getting called back . This will not called after your document is downloaded as because you are doing all the operation in the same page, load function will be called on loading of the page or refresh.

You can try some other option like display the loading div on calling of get pdf pho function and hide it off when that function complete its operations

Hope it helps
Vidhya Raju 25-Mar-15 1:46am
I tried php program in a separate file then too same issue exists. The page doesn't unload though beforeunload function getting triggered
ramyajaya 25-Mar-15 6:44am
if you are ready to use two php then $(window).load function is not needed.
it flows like this

Currentwindow.php-> on button click-> window.unload display loading msg-> download.php does download operation and in end it redirect to current windows.php
Vidhya Raju 25-Mar-15 6:56am
But I need beforeunload and load function to display loading icon while redirecting into another page. Just am facing issue when downloading a file. why beforeunload function is getting triggered when I try to download file? No way to solve it?
ramyajaya 25-Mar-15 7:57am
Instead of beforeunload try unload event since thise working of this event differs from browser to browser .

Reference :
Vidhya Raju 25-Mar-15 8:14am
unload event is not working as it was deprecated. Only beforeunload works.
ramyajaya 25-Mar-15 13:36pm
Try to use this
$(window).on('unload', function(event) {

Instead of beforeunload

This shows the msg when you navigate out of page not on any event occurrence within the page so that your get method will be called first
Pankaj Nikam 14-Feb-18 21:17pm
Hi there,

Were you able to solve this issue? I am having the same issue.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190114.1 | Last Updated 14 Feb 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100