[英]How to close the webpage on its own on clicking a button
我有一個頁面,其中保留了一個<button>
, onClick()
事件會在該頁面上觸發並在新選項卡中重定向新頁面。 同時,我希望在單擊<button>
並在新選項卡中打開新頁面后,父頁面立即關閉。 它基本上用於網站的某些安全功能。 如何才能做到這一點? 這是我的<button>
,讓我知道我可以在哪里糾正自己。
<button formtarget="_blank" type="submit" name="submit" onClick="javascript:window.open('quiz.php?unit_id=<?php echo $fnc->encode($unit_id) ; ?>');self.close();" value="submit" class="btn btn-info" style='margin-left: 35%;margin-bottom: 10px;' ><i class="glyphicon"></i> Start Quiz</button>
您可以通過以下功能關閉窗口
function close(){
setTimeout("window.close()", 500);
}
但請記住:腳本可能只關閉由它打開的窗口。
窗口關閉並打開新標簽
<script language="javascript">
function quitWindow(cmd)
{
if(window.open("your_new_tab_page.htm")){
if (cmd=='quit')
{
open(location, '_self').close();
}
}
return false;
}
</script>
<input type="submit" onclick="return quitWindow('quit');" value="Close This Window and open new Tab" />
如果你使用 firefox,你應該寫 url bar 並設置 about:config
dom.allow_scripts_to_close_windows = true
否則不工作火狐
我認為這可能會奏效。
<script>
function openWindow( url )
{
window.open(url, '_blank');
window.focus();
window.close();
}
</script>
<button onclick="javascript:openWindow(yourHref);return false;">Submit</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.