javascript - 检测用户是否单击了弹出窗口中的元素

标签 javascript jquery html popup intervals

是否可以检查用户是否单击了弹出窗口中的元素?我目前使用此 JavaScript 打开弹出窗口并检测它是否关闭。

var win = window.open(url,'','height=500,width=800');
var winTimer = window.setInterval(function()
{
    if (win.closed == true) {
        clearInterval(winTimer);
    }
}, 2000);

例如,我在弹出窗口中打开 YouTube,我想检测用户是否点击了“喜欢”按钮。如何检测间隔内的元素点击并在检测到后关闭弹出窗口?

最佳答案

不幸的是,除非该域托管在与您的网站相同的域上,否则您将遇到 cross-domain security问题。在这种情况下,您想要完成的任务(通过 youtube)将不起作用,除非他们有一个隐式 api 允许这样做(请参阅 Window.postMessage() )。

来自wikipedia page for the Same-origin_policy :

The main reason to have this restriction is because without the same-origin policy there would be a security risk. Assume that a user is visiting a banking website and doesn't log out. Then he goes to any random other site and that site has some malicious JavaScript code running in the background that requests data from the banking site. Because the user is still logged in on the banking site, that malicious code could do anything on the banking site. For example, get a list of your last transactions, create a new transaction, etc. This is because the browser can send and receive session cookies to the banking website based on the domain of the banking website. A user visiting that malicious site would expect that the site he is visiting has no access to the banking session cookie. While this is true, the JavaScript has no direct access to the banking session cookie, but it could still send and receive requests to the banking site with the banking site's session cookie, essentially acting as a normal user of the banking site. Regarding the sending of new transactions, even CSRF protections by the banking site have no effect, because the script can simply do the same as the user would do. So this is a concern for all sites where you use sessions and/or need to be logged in. If the banking site from the example (or any other site of course) only presents public data and you cannot trigger anything, then there is usually no danger which the same-origin policy protects against. Also, if the two sites are under control of the same party or trust each other completely, then there is probably no danger either.

关于javascript - 检测用户是否单击了弹出窗口中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38792837/

相关文章:

javascript - 将子类别添加到类别数组

javascript - 无法让 $.ajax.mostRecentCall 与 jasmine 2.0.2 一起使用

javascript - 需要帮助尝试在 native react 中刷新我的 token

jquery - JQuery回调方法不一致

html - Polymer 无法在 IE 11 上使用模板重复生成 HTML 表格

javascript - 对 js 文件中的 html 内容执行 javascript

html - 如何内嵌 3 个 div,最后使用自动宽度?

javascript - 如何使用用户定义的值来增加 Javascript 中的值

javascript - 启用当前页面链接的materializecss分页

c# - 从 ajax 发送一个空数组在 ASP.NET MVC 操作中给出了一个空参数