PHP Script in IFRAME Blocks Other Code

前端 未结 1 580
鱼传尺愫
鱼传尺愫 2020-12-11 09:29

Scenario:

I have two PHP scripts to be called simultaneously:

  1. The first script will run several minutes (PHP based file download), depending on downl
相关标签:
1条回答
  • 2020-12-11 09:40

    Your problem is as simple as you can imagine. You just don't realize it maybe for a bit lack of knowledge of HTML. So Your code is ok and everything is working as you want but the script that should run at the same time isn't, what is the problem?

    <body onload="callScripts();">

    This up here is your problem. The onload call only takes place when everything inside the body tag is completely loaded. So, as your iframe is inside the body the html interpreter load everything (including the iframe and its source), then call your callScripts function.

    To solve your problem I recommend you to create your iframe inside your script. Would be something as this:

    <!DOCTYPE html>
    <html>
    <head>
        <title>Title of the document</title>
    
        <!-- You should always define your script in the head tag. best pratice
         defined in W3C -->
    
    <script type="text/javascript">
    
        callScripts = function (){
             //write your Iframe Here
             document.getElementById("callDownload").innerHTML = '<iframe src="PHP/fileDownload.php"></iframe>'; 
             callScripts_refresh();
        }
    
        // call both PHP scripts(download and monitoring) in desired order
        callScripts_refresh = function()
        {
    
            // call the monitoring PHP script multiple times in 2 second intervals
            window.setTimeout(function(){startDownloadMonitoring()}, 1000);
            window.setTimeout(function(){startDownloadMonitoring()}, 3000);
            window.setTimeout(function(){startDownloadMonitoring()}, 5000);
            window.setTimeout(function(){startDownloadMonitoring()}, 7000);
            window.setTimeout(function(){startDownloadMonitoring()}, 9000);
        };
    
    
        // call monitoring PHP script via AJAX
        function startDownloadMonitoring()
        {
            console.log("Calling startDownloadMonitoring()...");
    
            var xmlhttp;
    
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
    
            xmlhttp.onreadystatechange = function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    console.log("Response Received: " + xmlhttp.responseText);
                }
            }
            xmlhttp.open("GET", "PHP/fileDownloadStatus.php", true);
            xmlhttp.send();
        }
    </script>
    
    </head>
    
    <body onload="callScripts();">
    
    <div id="callDownload"></div>
    
    </body>
    </html>
    

    Let me know if it work after that

    0 讨论(0)
提交回复
热议问题