简体   繁体   English

通知用户 JavaScript 中的跨域重定向

[英]Inform user about cross domain redirection in JavaScript

I have a requirement to fire up a confirm box method upon a click of any anchor tag based link in my application if that redirects the user to a different domain.如果将用户重定向到不同的域,我需要在单击我的应用程序中任何基于锚标记的链接时启动确认框方法。

So lets suppose I am using https://test.com and I click on a anchor tag based link所以让我们假设我正在使用https://test.com并单击基于锚标记的链接

<a onclick='window.open('https://www.google.co.in','_blank')>Click Me</a>

Then I need to fire up a custom confirmation popup method which if user confirms it redirects the user to the mentioned link.然后我需要启动一个自定义确认弹出方法,如果用户确认它会将用户重定向到提到的链接。

Edit: I would like to keep my anchor tag the way it is above.编辑:我想保持我的锚标签如上所示。 Is there some way I can capture this event and then fire up my method.有什么方法可以捕获此事件,然后启动我的方法。

You could do with having a function which runs on the "onclick" event, which then contains the code which pops up the "confirm" box.您可以使用在“onclick”事件上运行的函数,然后该函数包含弹出“确认”框的代码。

Demo:演示:

 function openWindowWithConfirm(url) { if (confirm("Are you sure you want to go to " + url + "?")) { window.open(url, '_blank'); } }
 <a onclick='openWindowWithConfirm("https://www.google.co.in")'>Click Me</a>

PS Terminology note: opening a new window is not the same as "redirection". PS 术语说明:打开新窗口与“重定向”不同。 A redirect is actually normally a term for a header sent out by the server in response to a request, telling the browser to go somewhere else instead.重定向实际上通常是服务器响应请求而发送的标头的术语,它告诉浏览器转到其他地方。 But people sometimes use it to mean doing a client-side navigation of the current window to a new URL.但是人们有时使用它来表示将当前窗口的客户端导航到新的 URL。 Certainly though, opening a new window doesn't redirect anything - the existing window stays on the same page.当然,打开一个窗口不会重定向任何东西 - 现有窗口保持在同一页面上。

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

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