无法访问此站点 (ERR_CONNECTION_CLOSED) javascript 检测 window.open
This site cant be reached (ERR_CONNECTION_CLOSED) javascript detection for window.open
我有一段 javascript 代码用于我的点击,它应该可以启用正确的点击跟踪。 clickDestinations各不相同,而且有很多(跨域)。
var response = window.open(clickDestination, randomName);
if (typeof response.focus === 'function') {
alert('tracking this click-out');
}
此实现的问题是 clickDestination 是由用户提供的,其中一些非常旧,因此无法保证正确设置了 http 或 https 协议。
当使用错误的协议调用 window.open 时,例如。在不支持 https 的网站上使用 https,我得到 "This site can’t be reached" 页面 (ERR_CONNECTION_CLOSED)。但是我的跟踪器无论如何都会跟踪,因为 var 响应是一个 window 对象。
在这种情况下如何检测错误而不跟踪有什么想法吗?
如果 url 在同一个域上(此处适用同源策略),第一个想法有效:
var w = window.open(url);
// if window opened successfully
if ( w ) {
w.onload = function() {
alert('tracking this click-out');
};
}
第二个想法:
window.open
returns a reference to the newly created window.
If the call failed, it will be null
instead. Ref.
因此,如果由于指定 URL 的服务器不支持 https 或 http null
而导致连接失败,那么您可以使用此信息跳过您的跟踪代码。
示例(未测试):
var response = window.open(clickDestination, randomName);
// if destination cannot be open, skip tracking code
if(!response){
return;
}
if (typeof response.focus === 'function') {
alert('tracking this click-out');
}
我有一段 javascript 代码用于我的点击,它应该可以启用正确的点击跟踪。 clickDestinations各不相同,而且有很多(跨域)。
var response = window.open(clickDestination, randomName);
if (typeof response.focus === 'function') {
alert('tracking this click-out');
}
此实现的问题是 clickDestination 是由用户提供的,其中一些非常旧,因此无法保证正确设置了 http 或 https 协议。
当使用错误的协议调用 window.open 时,例如。在不支持 https 的网站上使用 https,我得到 "This site can’t be reached" 页面 (ERR_CONNECTION_CLOSED)。但是我的跟踪器无论如何都会跟踪,因为 var 响应是一个 window 对象。
在这种情况下如何检测错误而不跟踪有什么想法吗?
如果 url 在同一个域上(此处适用同源策略),第一个想法有效:
var w = window.open(url);
// if window opened successfully
if ( w ) {
w.onload = function() {
alert('tracking this click-out');
};
}
第二个想法:
window.open
returns a reference to the newly created window. If the call failed, it will benull
instead. Ref.
因此,如果由于指定 URL 的服务器不支持 https 或 http null
而导致连接失败,那么您可以使用此信息跳过您的跟踪代码。
示例(未测试):
var response = window.open(clickDestination, randomName);
// if destination cannot be open, skip tracking code
if(!response){
return;
}
if (typeof response.focus === 'function') {
alert('tracking this click-out');
}