无法添加事件监听器

无法添加事件监听器

本文介绍了无法添加事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我可以将事件的侦听器添加到空白,而不添加到 twitter



i can add an event listener for clicks to blank but not to twitter in the code below.

const blank = window.open();
const twitter = window.open("https://twitter.com");

const PrintClick = function (name) {
    return function (...args) {
        console.log(name, ...args);
    };
};

blank.addEventListener("click", PrintClick("blank"));
twitter.addEventListener("click", PrintClick("twitter"));

是因为Twitter做了一些不允许我做的事情吗?

is it because twitter has done something to not let me do this? would there be a way to get around it?

推荐答案

您没有遇到任何异常的原因:



大多数浏览器不支持多个弹出窗口,因此要完成此操作,您需要尝试使用:

The reason that you did not got any exception :

Most browsers don't support multiple popups so in order to accomplish it wou need to try using:

window.open(yoururl,"_blank",'PopUp',randomnumber,'scrollbars=1,menubar=0,resizable=1,width=850,height=500');

或者给每个窗口。

window.open(url, WindowName)



安全风险

不能使用JavaScript添加来源不同的事件列表,如果这样做的话,将是一个巨大的安全漏洞。对于 浏览器阻止试图访问具有不同来源的框架的脚本

Security Risk

You can't add an event listner with different origin using JavaScript, it would be a huge security flaw if you could do it. For the same-origin policy browsers block scripts trying to access a frame with a different origin.

如果未保留地址的以下至少其中之一,则认为来源不同:

Origin is considered different if at least one of the following parts of the address isn't maintained:

<protocol>://<hostname>:<port>/...

协议主机名端口必须与您的域相同您要访问框架。

Protocol, hostname and port must be the same of your domain, if you want to access a frame.

这是尝试从 http访问以下URL时将发生的情况: //www.example.com/home/index.html

URL                                             RESULT
http://www.example.com/home/other.html       -> Success
http://www.example.com/dir/inner/another.php -> Success
http://www.example.com:80                    -> Success (default port for HTTP)
http://www.example.com:2251                  -> Failure: different port
http://data.example.com/dir/other.html       -> Failure: different hostname
https://www.example.com/home/index.html:80   -> Failure: different protocol
ftp://www.example.com:21                     -> Failure: different protocol & port
https://google.com/search?q=james+bond       -> Failure: different protocol, port & hostname

不推荐

我将链接相对答案。但是,请记住,禁用同源策略只会影响您的浏览器。此外,运行禁用了同源安全设置的浏览器会授予任何网站访问跨域资源的权限,因此这是非常不安全的,如果您不完全了解自己的身份,则绝对不要这样做做(例如出于开发目的)

I'll link the relative answer. However, please remember that disabling the same-origin policy will only affect your browser. Also, running a browser with same-origin security settings disabled grants any website access to cross-origin resources, so it's very unsafe and should NEVER be done if you do not know exactly what you are doing (e.g. development purposes).






  • Microsoft Edge:


  • Google Chrome
  • Mozilla Firefox
  • Safari
  • Opera
  • Microsoft Edge: not possible
  • Microsoft Internet Explorer

这篇关于无法添加事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 06:52