Javascript IFRAME中的PHP脚本会阻止其他代码 脚本:
我需要同时调用两个PHP脚本:Javascript IFRAME中的PHP脚本会阻止其他代码 脚本:,javascript,php,ajax,iframe,wamp,Javascript,Php,Ajax,Iframe,Wamp,我需要同时调用两个PHP脚本: 第一个脚本将运行几分钟(基于PHP的文件下载),具体取决于下载的文件大小。它被放置在中,因此可以单独运行,并且不会阻塞浏览器 第二个PHP脚本应该定期调用,以监视第一个脚本文件下载进度的执行。为了避免在脚本完成时打开新窗口,通过AJAX调用它 问题: 我已将长期运行的PHP脚本(下载脚本)放入中,以便该脚本可以与其他监视PHP脚本异步运行。但是,尽管主脚本位于中,但当网页开始执行时,脚本会启动并阻止剩余JavaScript代码的执行,并通过AJAX多次调用监控脚本
中,因此可以单独运行,并且不会阻塞浏览器
中,以便该脚本可以与其他监视PHP脚本异步运行。但是,尽管主脚本位于
中,但当网页开始执行时,脚本会启动并阻止剩余JavaScript代码的执行,并通过AJAX多次调用监控脚本
短时间运行的监视PHP脚本与长时间运行的PHP(下载)脚本同时调用非常重要,因此短时间运行的(监视)脚本可以向JavaScript提供反馈
请您分析一下我的代码样本好吗?我不知道,我的问题在哪里。我的代码非常简单,所有东西都应该运行良好
- PHP版本5.4.12
- Apache/2.4.4(Win64)PHP/5.4.12
- Windows 7 x64
- 8GB内存
- 谷歌浏览器版本30.0.1599.101 m
文件标题
//按所需顺序调用两个PHP脚本(下载和监视)
callScripts=function()
{
//在2秒钟的时间间隔内多次调用监视PHP脚本
setTimeout(函数(){startDownloadMonitoring()},1000);
setTimeout(函数(){startDownloadMonitoring()},3000);
setTimeout(函数(){startDownloadMonitoring()},5000);
setTimeout(函数(){startDownloadMonitoring()},7000);
setTimeout(函数(){startDownloadMonitoring()},9000);
};
//通过AJAX调用监控PHP脚本
函数startDownloadMonitoring()
{
log(“调用startDownloadMonitoring()…”;
var-xmlhttp;
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
log(“收到的响应:+xmlhttp.responseText”);
}
}
open(“GET”,“PHP/fileDownloadStatus.PHP”,true);
xmlhttp.send();
}
PHP监控脚本(fileDownloadStatus.PHP)
PHP长时间运行脚本(fileDownload.PHP)
截图:
PHP脚本执行顺序-浏览器在
你的问题很简单,你可以想象得到。你只是没有意识到这一点,也许是因为你对HTML有点缺乏了解。因此,您的代码是正常的,并且一切都按照您想要的方式工作,但是应该同时运行的脚本不是,问题是什么
这是你的问题。onload
调用仅在主体
标记中的所有内容都已完全加载时发生。因此,由于iframe
位于body
内,html解释器将加载所有内容(包括iframe及其源代码),然后调用callScripts
函数
为了解决您的问题,我建议您在脚本中创建iframe
。应该是这样的:
<!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>
文件标题
callScripts=函数(){
//在这里写下你的Iframe
document.getElementById(“callDownload”).innerHTML='';
callScripts_refresh();
}
//按所需顺序调用两个PHP脚本(下载和监视)
callScripts\u refresh=函数()
{
//在2秒钟的时间间隔内多次调用监视PHP脚本
setTimeout(函数(){startDownloadMonitoring()},1000);
setTimeout(函数(){startDownloadMonitoring()},3000);
setTimeout(函数(){startDownloadMonitoring()},5000);
setTimeout(函数(){startDownloadMonitoring()},7000);
setTimeout(函数(){startDownloadMonitoring()},9000);
};
//通过AJAX调用监控PHP脚本
函数startDownloadMonitoring()
{
log(“调用startDownloadMonitoring()…”;
var-xmlhttp;
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
log(“收到的响应:+xmlhttp.responseText”);
}
}
open(“GET”,“PHP/fileDownloadStatus.PHP”,true);
xmlhttp.send();
}
如果可以的话,请告诉我它是否能工作,我建议使用XHR2。现在可以通过AJAX发送文件@你好,谢谢。据我所知,我只能通过AJAX上传文件,但不能下载。由于安全原因,如果我正确研究了这个主题,通过AJAX下载文件会被阻止。如果您正确设置了Access Control Allow Origin标头,您基本上可以在两个URL之间执行任何您想要的操作。你研究过了吗?@teh1,我正在研究。退出大量文本。如果你能给我指出正确的部分,那会有帮助的。该页面上没有关于文件下载的内容。我知道
<?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
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();
}
?>
<!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>