简体   繁体   English

如何在浏览器打开新标签页时捕获目标 url

[英]How to capture on browser open new tab the target url

Is it possible to catch the attribute of hyperlink with JavaScript or Jquery on browsers open new tab event?是否可以在浏览器打开新标签事件中使用 JavaScript 或 Jquery 捕获超链接的属性?

lets say I have a hyperlink <a href="http://example.com/something">MyLink</a> and using right click on link and opening new tab should alert first " http://example.com "假设我有一个超链接<a href="http://example.com/something">MyLink</a>并且使用右键单击链接并打开新标签应该首先提醒“ http://example.com

I think this is only possible with writing a browser extension.我认为这只能通过编写浏览器扩展来实现。 You cant control this type of things with JavaScript.你不能用 JavaScript 控制这种类型的东西。

Ok, so this is the best that I can come up with but it ain't pretty.好的,所以这是我能想到的最好的,但它并不漂亮。

Have all the links on the page point to another page (we'll call it redir.html).让页面上的所有链接都指向另一个页面(我们将其称为 redir.html)。 redir.html is passed a parameter via the URL that defines what page it should then open, like redir.html?page=www.example.com/something . redir.html 通过定义它应该打开的页面的 URL 传递一个参数,如redir.html?page=www.example.com/something redir.html then executes whatever JavaScript you want, perhaps only if it was opened in a new tab/window.然后 redir.html 执行您想要的任何 JavaScript,也许只有在新选项卡/窗口中打开它。 This can be detected by checking to see if window.history.length === 1 .这可以通过检查window.history.length === 1来检测。

redir.html: redir.html:

<script>
function getParameter(param) {
            var val = document.URL;
            var url = val.substr(val.indexOf(param))  
            var n=url.replace(param+"=","");
            return n;
}
var page = getParameter("page");
if(window.history.length === 1)
{
    alert("I opened in a new tab or window");
}

window.location.href = page;
</script>

Example anchor tag on source page:源页面上的示例锚标记:

<a href="redir.html?page=http://example.com/something">My link</a>

(I got the getParameter function from this SO post .) (我从这个 SO post得到了getParameter函数。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM