javascript - 当用户在新窗口中打开时,如何使 onclick"window.location" 也能工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3926119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:40:33  来源:igfitidea点击:

javascript - How do I make onclick"window.location" also work when user opens in new window

javascriptjavascript-events

提问by Steven

To hide the true destination of a link I'm using the following code:

要隐藏链接的真实目的地,我使用以下代码:

<a href="http://example.com" onclick="window.location='http://url-i-want-to-hit.com/'; return false;">Click me</a>

However, if I do right-click and then open in new window, it goes to the url specified in the anchor href tag. I want it to go to the url specified in the javascript.

但是,如果我右键单击然后在新窗口中打开,它会转到锚点 href 标记中指定的网址。我希望它转到 javascript 中指定的 url。

Is there a way to do this?

有没有办法做到这一点?

Thanks in advance

提前致谢

回答by Josh Stodola

All you need is a mousedown handler to fake them out...

你所需要的只是一个 mousedown 处理程序来伪造它们......

<a href="http://example.com"
   onmousedown="this.href2 = this.href;
                this.href = 'http://url-i-want-to-hide.com/';"
   onmouseout="if(this.href2) this.href = this.href2;">Test</a>

And here is a live demoof it working.

这是它工作的现场演示

回答by Pekka

However, if I do right-click and then open in new window, it goes to the url specified in the anchor href tag. I want it to go to the url specified in the javascript.

但是,如果我右键单击然后在新窗口中打开,它会转到锚点 href 标记中指定的网址。我希望它转到 javascript 中指定的 url。

No, but you could use JS to set the link href to the hidden URL after the document has loaded (or in the onfocusand onhoverevents of the link, although that feels rather kludgy and incomplete).

不,但您可以在文档加载后(或在链接的onfocusonhover事件中,尽管感觉相当笨拙和不完整)使用 JS 将链接 href 设置为隐藏的 URL 。

<a onfocus="this.href='hidden_url'" .....>

this is the way Google use to count outgoing links from the search results page.

这是谷歌用来计算来自搜索结果页面的传出链接的方式。

回答by Drew

You can't do this. Right click uses the true anchor, whereas you are overriding the click functionality of the anchor with javascript. A bit deceptive aren't we?

你不能这样做。右键单击使用真正的锚点,而您使用 javascript 覆盖锚点的单击功能。有点欺骗我们不是吗?

回答by Graeme Smith

How about storing the real urls in a database and calling them in php?

如何将真实的 url 存储在数据库中并在 php 中调用它们?

www.yourwebsite.com/url.php?id=3

url.php // something like

url.php // 类似

$query = "SELECT * FROM `urls` WHERE `id` = " . intval($_GET['id']);
$row = mysqli_fetch_assoc($query);

header ("location: $row['url']");

回答by AGoodDisplayName

EDIT: Maybe I wasn't clear...sorry. Using an "#" would prevent them opening a different page in a new window. This may not work for your situation, and I don't know if you are trying to get something accomplished with SEO by hidding the URL. My solution is virtually the same as what you are currently doing except for the "#" in the href and I just prefer to have the window.location wrapped in a function....personal preference.

编辑:也许我不清楚......对不起。使用“#”会阻止他们在新窗口中打开不同的页面。这可能不适用于您的情况,我不知道您是否试图通过隐藏 URL 来通过 SEO 来完成某些事情。除了href中的“#”之外,我的解决方案几乎与您当前正在做的相同,我只是更喜欢将window.location包装在一个函数中......个人偏好。

<a href="#" onclick="SendToUrl(); return false;"">Click Here</a>



<script language="javascript">
   function SendToUrl(){
       window.location='http://url-i-want-to-hide.com/'
    }
</script>