简体   繁体   中英

javascript how to know the target page before redirect the whole page

Well, it should be easy.

Assume I have a page and there are multiple links and buttons on it.

Those links and buttons may links to anywhere in the internet.

Now, what I am trying to do is to check, after user clicking a link or button, to where the page is going to be redirected. If the target page is within the same domain of current page's domain, then allow the redirecting, otherwise stop it.

Is there anyway to do it? I mean I cannot retrieve the URL before the clicking, and there are a lot of buttons which I cannot write function for each of them. I need to single function which can monitor all the ongoing redirecting action.

I know there is a 'onbeforeunload' function which allow me to do the something before the current page is going to be unloaded. However, I don't know where can I find the target URL.

Is there anyone have idea about this?

Thanks in advance!

It can't be done using onbeforeunload. The new location is private/sensitive information. Who would want you to know which sites they visit when they leave your site.

The only way would be to try and hook everything on the page, links, buttons, etc and even then you really want to do that to your users? Invading their privacy is a sure way to make them very upset.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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