Hi, colleagues

I would appreciate your opinion/advice on the following

I'm trying to save extra run to the back end by overwriting JS url
variable with AJAX function and activate download from the visitor's
browser as described by code below.

Unfortunately what happens is extra run to the back end happens in any
case in spite of the fact that AJAX runs successfully.

Do you think it's related to the bug ignoring "async: false,"
definition or it's mistake I've made and missed to catch?

Thank you in advance

HTML code
=========
<a href="/download.php?what=PDF_A" onclick="javascript:download
('PDF_A')">Download</a>

JS code
=======
function download ( what )  {

    var url = "download.php";

    $.ajax({
       type: "GET",
       url: "download.php?ajax=true",
       data: "what=" + what
       async: false,
       dataType: "script"
   });

   // if AJAX got the download URL, actual download starts, otherwise
do that on the back end:
   location.href = url;
}

Back end (download.php) code
=======================
$myPDF = array();
$myPDF["PDF_A"] = "PDF_A.pdf";
....
$url = "http://mysite.com/"; . $myPDF["PDF_A"];
...
if ( $_GET["ajax"] === "true" ) {
     // overwrite JS url variable
     print('url = "'.$url.'";');
} else {
     header("Location: ". $url );
     header("Connection: close");
}

Reply via email to