如何检测天气用户已单击 jQuery 中新窗口上的打开链接

问题描述 投票:0回答:1

无论浏览器或 html 标签如何,如果用户单击新窗口中的打开链接,则用户应该被重定向到登录页面。

我已经尝试过 window.opener 但它不起作用

在完整的网站中有不同的标签,用户右键单击并获得在新窗口上打开链接的选项

我已经尝试过 window.opener 但它不起作用

在完整的网站中有不同的标签,用户右键单击并获得在新窗口上打开链接的选项

我有某个地方的跨度标签或许多不同的标签,用户可以在其中获得此选项

javascript c# jquery .net asp.net-mvc-4
1个回答
0
投票

首先:这不是一个好主意 有很多原因

第二:如果你还想这样做

这是一个例子:

const elements = document.getElementsByTagName('a');

for (let i = elements.length - 1; i >= 0; --i) {
    // only when the user use the context menu
    elements[i].addEventListener('contextmenu', (event) => {
        const target = event.target;
        // store the original href
        const originalHref = target.href;

        // change the href redirect the use
        target.href = '/login';

        // change back to the original href
        target.addEventListener('blur', () => {
            target.href = originalHref;
            // remove the event listener to do it just once
            target.removeEventListener('blur', this);
        })
    })
}

一些解释:

javascript .addEventListener('contextmenu' 

你可以用这个检测上下文菜单

javascript event.target.href = '/login'; 

单击链接之前交换 href

javascript .addEventListener('blur' 

这将检测用户何时离开上下文菜单并返回页面

javascript event.target.removeEventListener( 

删除监听器,因为它现在没用了

记住...这不是一个好主意

© www.soinside.com 2019 - 2024. All rights reserved.