PHP脚本在IFRAME阻止其他code [英] PHP Script in IFRAME Blocks Other Code

查看:151
本文介绍了PHP脚本在IFRAME阻止其他code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个PHP脚本被同时调用:

I have two PHP scripts to be called simultaneously:

  1. 在第一个脚本将运行几分钟(基于PHP文件下载),根据下载的文件大小。它被放置到< IFRAME> ,因此它可以单独运行,并不会阻止浏览器
  2. 第二个PHP脚本应该被称为定期监督执行的第一个脚本 - 文件进行下载。为了避免打开脚本完成后,新的窗口,它是通过AJAX调用。
  1. The first script will run several minutes (PHP based file download), depending on downloaded file size. It is placed into <iframe> so it can run separately and does not block the browser.
  2. The second PHP script is supposed to be called in regular intervals to monitor execution of the first script - file progress download. To avoid opening new windows upon script completion, it is called via AJAX.

我已经把长期运行的PHP脚本(下载脚本)到&LT; IFRAME&GT; 因此该脚本可以与其他监测PHP脚本异步运行。然而,尽管主脚本是在&LT; IFRAME&GT; ,当网页开始执行,该脚本将启动,并阻止执行剩余的JavaScript code和监控脚本调用通过AJAX多次。

Problem:

I have placed the long-running PHP Script (download script) into <iframe> so this script can run asynchronously with other monitoring PHP script. However, despite the main script is in <iframe>, when the webpage starts execution, the script starts and blocks execution of the remaining JavaScript code and monitoring script called multiple times via AJAX.

有同时调用长期运行PHP(下载)脚本短期运行监控PHP脚本是很重要的,所以短期运行(监测)脚本可以提供反馈给JavaScript。

It is important to have the short-running monitoring PHP script called simultaneously with the long-running PHP (download) script, so the short-running (monitoring)script can provide feedback to JavaScript.

你会这么好心,分析我的code样本吗?我不知道,这里是我的问题。我的code是如此简单,这一切都应该运行良好。

Would you be so kind and analyze my code samples please? I have no idea, where is my problem. My code is so simple, that everything should be running well.

  • 在PHP版本5.4.12
  • 的Apache / 2.4.4(Win64中)PHP / 5.4.12
  • 在Windows 7的64
  • 8GB内存
  • 在谷歌浏览器版本30.0.1599.101米

JavaScript的code调用这两个PHP脚本:

<!DOCTYPE html>
<html>
<head>
    <title>Title of the document</title>
</head>

<body onload="callScripts();">


<script type="text/javascript">

    // call both PHP scripts(download and monitoring) in desired order
    callScripts = 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>

<iframe src="PHP/fileDownload.php"></iframe>

</body>
</html>

的PHP脚本监控(fileDownloadStatus.php)

<?php

include 'ChromePhp.php';

// start session, update session variable, close session
session_start();
$_SESSION['DownloadProgress']++;
ChromePhp::log('$_SESSION[\'DownloadProgress\'] = ' . $_SESSION['DownloadProgress']);
session_write_close();    

echo "success";
?>

PHP长期运行脚本(fileDownload.php)

<?php
include 'ChromePhp.php';

// disable script expiry
set_time_limit(0);    

// start session if session is not already started
session_start();

// prepare session variables
$_SESSION['DownloadProgress'] = 10;

session_write_close();

for( $count = 0; $count < 60; $count++)
{
    sleep(1);

    print("fileDownload Script was called: ". $count);

    echo "Download script: " . $count;
    ob_flush();
    flush();
}
?>

截图:

的PHP脚本执行顺序 - 浏览器等待完成脚本&LT; IFRAME&GT;

Screenshot:

PHP Scripts Execution Order - browser waits to finish the script in <iframe>

推荐答案

您的问题很简单,只要你能想象的。你只是没有意识到这一点也许是因为有点缺乏HTML知识。所以,你的code是确定,因为你想要的一切工作正常,但应该运行的同时剧本是不是有什么问题?

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?

&LT;身体的onload =callScripts();&GT;

这在这里是你的问题。该的onload 只调用发生时,一切体内标签是完全加载。因此,当你的 IFRAME 体内的HTML除preTER负载的一切(包括IFRAME和其源),然后打电话给你的 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.

要解决你的问题,我建议你创建 IFRAME 你的脚本中。会是因为这样:

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

这篇关于PHP脚本在IFRAME阻止其他code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆