我在我的頁面正文中有以下代碼。它應該在新窗口中打開登錄頁面,然後關閉當前窗口。javascript window.open無法正常工作時_blank
<script type="text/javascript">
window.open('/login', '_blank');
window.close();
</script>
如果我改變_blank
到_self
但這不是我想要的它的工作原理!
任何想法?
我在我的頁面正文中有以下代碼。它應該在新窗口中打開登錄頁面,然後關閉當前窗口。javascript window.open無法正常工作時_blank
<script type="text/javascript">
window.open('/login', '_blank');
window.close();
</script>
如果我改變_blank
到_self
但這不是我想要的它的工作原理!
任何想法?
問題
恐怕你瀏覽器不允許這樣的腳本阻止彈出窗口。
解決方案
您的瀏覽器(和他的彈出窗口攔截器),如果用戶事件(如點擊)的加工過程中使用通常只允許window.open。所以你應該嘗試重新設計你的腳本行爲。
也許另一種有趣的方法
也許你應該考慮使用一個不錯的彈出式的jQuery插件像Fancybox。這不會被您的瀏覽器阻止,因爲它不是真實彈出。
你可以寫window.open('/login')
而不是window.open('/login', '_blank')
。
window.open()
只會打開新的窗口,沒有必要的_blank
或_self
window.open('/login');
從技術文檔:
window.open(URL,name,specs,replace)
所以關於名字的說法:
Optional. Specifies the target attribute or the name of the window. The following values are supported:
_blank - URL is loaded into a new window. This is default
_parent - URL is loaded into the parent frame
_self - URL replaces the current page
_top - URL replaces any framesets that may be loaded
name - The name of the window (Note: the name does not specify the title of the new window)
所以你不需要_blank因爲默認的
window.open默認工作功能的新窗口/標籤 – Lab