简体   繁体   English

弹出窗口处于活动状态时,在启动时自动加载页面

[英]Auto-loading page on start up into pop-up window when pop-up window is active

Hard to come up with a title, my apologizes. 抱歉,想出一个标题。

Problem is this: Since modern web-browsers disable pop-up windows I am in need of a work-around. 问题是这样的:由于现代的网络浏览器会禁用弹出窗口,因此我需要一种解决方法。

When a visitor comes to the website they are prompt to press a button. 访客进入网站时,会提示他们按下一个按钮。 Once the button is pressed a pop-up window is launched with the following code: 按下按钮后,将弹出一个带有以下代码的窗口:

w = window.open('/audio/audioplayer.php?id='+audioId, 'audioplayer', params);

Now that the pop-up is open I would like when the visitor views other pages the pop-up is loaded with specific information based on whatever page they are on. 既然弹出窗口是打开的,我希望当访问者查看其他页面时,弹出窗口会根据他们所在的页面加载特定信息。

I am not sure if this is possible or how I can do this (check if the pop-up window is open, and if it is load the information, and if its not re-display the button) 我不确定这是否可行或我该怎么做(检查弹出窗口是否打开,是否正在加载信息,以及是否未重新显示按钮)

I don't think it is possible to detect where the popup is open of not. 我认为不可能检测到弹出窗口未打开的位置。

Have you thought about using a dialog? 您是否考虑过使用对话框? Rather than a popup? 而不是弹出窗口?

window.open returns a windowObjectReference - this is the only way you can talk to the popup window. window.open返回windowObjectReference-这是您可以与弹出窗口对话的唯一方法。 In particular, you can tell if that window is closed with the windowObjectReference.closed attribute. 特别是,您可以使用windowObjectReference.closed属性判断该窗口是否关闭。 And the popup window has a window.opener attribute that references the parent window back. 弹出窗口具有一个window.opener属性,该属性引用了父窗口。 You can use both to communicate. 您可以使用两者进行交流。

However, it seems you want to keep this communication between page loads. 但是,您似乎希望在页面加载之间保持这种通信。 You have a few options: 您有几种选择:

  • Try to keep the link between windows as long as possible. 尽量保持窗口之间的链接尽可能长。 The problem is that when the parent window reloads, all the javascript variables reset and there's no way to recover the reference to the popup - unless the popup sets it using window.opener . 问题在于,当父窗口重新加载时,所有javascript变量都会重置,并且无法恢复对弹出窗口的引用-除非弹出窗口使用window.opener对其进行设置。 This link shows this approach and also another one with frames. 此链接显示了这种方法,还有一个带有框架的方法。
    You could consider it either ugly or clever. 您可能会认为它很丑或很聪明。 But it's not perfect. 但这并不完美。 (You can't do anything if the user opens a page in a new link) (如果用户在新链接中打开页面,则无法执行任何操作)
  • Communicate with the server using ajax from both main pages and the popup page. 在主页和弹出页面中都使用ajax与服务器进行通信。 When a top level page wants to send a message to the popup, they start an XMLHttpRequest to your server which notifies a script which leaves a message in a "queue". 当顶层页面想要将消息发送到弹出窗口时,它们会启动XMLHttpRequest到您的服务器,该服务器通知一个脚本,该脚本将消息保留在“队列”中。 The popup page regularly polls/long-polls the server with XHR too (or server sent events , my personal favorite) and updates its own contents accordingly. 弹出页面也定期使用XHR轮询/长期轮询服务器(或服务器发送的事件 ,我个人最喜欢),并相应地更新其自身的内容。
    This might be a bit more complex/expensive than you'd like but it's also the safest solution. 这可能比您想要的更复杂/更昂贵,但这也是最安全的解决方案。
  • Don't use popups, like the other answer suggested. 不要像其他建议的那样使用弹出窗口。 A div with position: fixed could get you a similar result, and might save you from that method of communication between windows, however it also leads to having one dialog per page, so you need to ask the server if another instance of the dialog is running. 具有position: fixed div可能会给您带来类似的结果,并且可能使您免于使用Windows之间的这种通信方法,但是它还会导致每页显示一个对话框,因此您需要询问服务器是否该对话框的另一个实例是运行。 Not quite sure if other methods of sync are viable for this (localstorage?) 不确定其他同步方法是否可行(本地存储?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM