使用 mootool 获取打开的页面/选项卡
我有一个网站需要知道用户是否打开了其他页面/选项卡只是为了在其中执行操作(例如关闭它们、重定向等)。我不是在谈论弹出窗口,而是在谈论用户在新页面选项卡中手动打开的内容。
所以基本上我需要一种方法来获取在我的域上打开的选项卡/窗口列表,并能够访问其中的内容(因为所有都是同一个域,不应该有任何安全问题)
是否存在类似的东西?
I have a site that need to know if the user have other pages/tabs opened just to make actions in them (like closing them, redirect etc..). I'm not talking about popups, I'm talking about content opened in a new page tab manually by the user.
So basically I need a method to get a list of tabs/windows opened on my domain and be able to access the content in them (as all is the same domain, there should not be any security problems)
Does something like that exist?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这可能是可能的,因为你说它们都在同一个域上。
当页面/选项卡加载时,您希望为其分配一个唯一的 ID 并将其发送到服务器。当它关闭时,您还需要告诉服务器,以便服务器知道哪些选项卡仍然打开。在每个打开的选项卡中,您可以定期向服务器查询您希望在此选项卡中发生的任何操作,例如重定向或关闭它。
I think it might be possible, since you say it's all on the same domain.
When a page/tab gets loaded, you want to assign it an unique id and send it to the server. When it gets closed you will need to tell the server about it as well, so the server knows what tabs are still open. In each open tab you can then regularly query the server for any actions that you want to happen in this tab, like redirecting or closing it.
实际上,这是可能的,并且您不需要不断地处理服务器请求。使用localStorage,并绑定到存储事件。实现实际上非常简单。唯一严重的问题是,并非所有浏览器都遵循是否在原始窗口上触发存储事件的规范,但只需几行代码,您就可以规范化行为。
Actually, this IS possible, and you don't need to go through constant server requests. Use localStorage, and bind to the storage event. The implementation is actually really simple. The only serious gotcha is that not all browsers follow the specification for whether the storage event is fired on the origin window, but with a few lines of code you can normalize the behavior.