我是php新手并试图弄明白,如何在保持其他网页打开的同时关闭一个打开的网页。例如,我想打开yahoo.com网页,如下面的代码所示。然后我想关闭这个yahoo.com页面并打开google.com页面。
<?php
//To open yahoo.com page
echo '<script type="text/javascript" language="javascript">
window.open("http://www.yahoo.com/");
</script>';
//How can I close the yahoo.com page and let the program keep running
echo "<script>window.close();</script>";
sleep(10);
//To open google.com page
echo '<script type="text/javascript" language="javascript">
window.open("http://www.google.com/");
</script>';
echo "Done";
?>
答案 0 :(得分:3)
这是一个JavaScript问题。您从PHP回显脚本标记的事实对答案没有影响。
window.open
的返回值是您创建的窗口的句柄。如果存储它,您可以稍后关闭它:
var w = window.open("http://www.yahoo.com/"); // Opens window with yahoo.com
w.close(); // Closes window with yahoo.com
jsFiddle for the above code snippet
请注意:(a)许多浏览器会阻止弹出窗口,(b)它会在打开后立即关闭窗口。如果您想睡在两者之间,请使用JavaScript的setTimeout
方法:
var w = window.open("http://www.yahoo.com/"); // Opens window with yahoo.com
setTimeout(function() { w.close() }, 1000); // Closes window with yahoo.com after waiting 1000 milliseconds
答案 1 :(得分:0)
简单的例子
<!DOCTYPE html>
<html>
<head>
<script>
function openWind()
{
myWindow=window.open("http://www.yahoo.com/","","width=1000,height=500");
}
function closeWind()
{
myWindow.close();
}
</script>
</head>
<body>
<input type="button" value="Open 'myWindow'" onclick="openWind()" />
<input type="button" value="Close 'myWindow'" onclick="closeWind()" />
</body>
</html>