[英]How to check if a web page is open or not?
I have a website where users message each other through another website.我有一个网站,用户通过另一个网站互相发送消息。 My problem is that everytime user click on message item this action opens a new tab.
我的问题是每次用户单击消息项时,此操作都会打开一个新选项卡。
Having a lot of conversations makes this quite annoying, especially when using on a smartphone.进行大量对话会使这很烦人,尤其是在智能手机上使用时。
Is there any way to check if the website for texting is open?有什么方法可以检查发短信的网站是否打开?
If you are using window.open() you can add the target parameter.如果您使用的是 window.open() 您可以添加目标参数。
A string, without whitespace, specifying the name of the browsing context the resource is being loaded into.
一个字符串,没有空格,指定资源正在加载到的浏览上下文的名称。 If the name doesn't identify an existing context, a new context is created and given the specified name.
如果名称不能识别现有上下文,则会创建一个新上下文并赋予指定的名称。 The special target keywords, _self, _blank, _parent, and _top, can also be used.
也可以使用特殊目标关键字_self、_blank、_parent 和_top。
example例子
window.open("https://www.mozilla.org/", "mozillaTab");
I hope it helps you:)我希望它可以帮助你:)
You are looking for postMessage .您正在寻找postMessage 。 Synapsis:
突触:
postMessage(message, targetOrigin, transfer)
message
is the actual message you want to send message
是您要发送的实际消息targetOrigin
specifies which domain is the target targetOrigin
指定哪个域是目标transfer
is a sequence of transferrable objects that are transmitted with the message transfer
是与消息一起传输的一系列可转移对象So, what you really want is to have this conversation:所以,你真正想要的是进行这样的对话:
or或者
I will call Page2 the page whose existence we wonder about and Page1 the page which wonders about Page2's existence.我将把 Page2 称为我们想知道其存在的页面,并将 Page1 称为想知道 Page2 存在的页面。
First of all, you need a message handler at both Page1 and Page2.首先,您需要在 Page1 和 Page2 上都有一个消息处理程序。 Synapsis:
突触:
window.addEventListener("message", (event) => {
if (event.origin !== "http://example.org:8080")
return;
//check event.data and see what happens
}, false);
On Page 2, your message handler will need to check whether the message asks about its existence and calls postMessage('Yes, I am here', event.origin);
在第 2 页,您的消息处理程序将需要检查消息是否询问其存在并调用
postMessage('Yes, I am here', event.origin);
if so.如果是这样。 Page1, on the other hand initiates the messaging by calling
postMessage('Are you there?', theurl);
另一方面,Page1 通过调用
postMessage('Are you there?', theurl);
来启动消息传递。 (where you need to replace theurl
with your value). (您需要用您的值替换
theurl
)。 Now, Page1 expects a message.现在,Page1 需要一条消息。 If the message arrives, then Page2 exists.
如果消息到达,则 Page2 存在。 If not, then after a timeout you need to handle the nonexistence of Page2.
如果没有,那么在超时后您需要处理 Page2 的不存在。 So, you will have something like this at Page1:
所以,你会在第 1 页有这样的东西:
var wasFound = false;
postMessage('Are you there', theurl);
setTimeout(function() {
if (!wasFound) {
//The page needs to be opened
}
}, 500);
Naturally, you will need to set wasFound
to true when you receive a message from Page2 and you will need to make sure that the message handler sees the same wasFound
variable that your timeout
checks for.自然,当您收到来自 Page2 的消息时,您需要将
wasFound
设置为 true,并且您需要确保消息处理程序看到与您的timeout
检查相同的wasFound
变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.