I have to check the the URL / href is leaving the domain or not? I have to redirect the leaving URL to a page where it shows the "You are leaving the domain" and then after some time need to redirect to external link/URL.

Eg. If I am on the domain=> http://test.com/Pages/test.aspx and then I've clicked on the URL/ Href => http://redirecttoexternaldomain/pages/welcome.aspx Then I have to show the message as above on the page => "You are leaving the domain" and then rediect to the external URL.

Is it possible even if the JQuery/ Java Script is disabled from the browser? How to do this with Programmatically & with JQuery?


1 Answer 1

  1. Create a middle page which get redirect url in query string and display message you are asking. Use javascript window.location.href = <redirecturlfromquerystring>

Now coming to all your source links.

Option 1 If you can put a css class on all your external class. you can bind click event to all your external links

Sample html

 <div id="commercial-container">
        <a href="http://www.link1.com" class="external" target="_blank"> <img src="picture1.jpg" /> </a>
        <a href="http://www.link2.com" class="external" target="_blank"> <img src="pciture2.jpg" /> </a>
        <a href="http://www.link3.com" class="external" target="_blank"> <img src="picture3.jpg" /> </a>
        <a href="http://www.link4.com" class="external" target="_blank"> <img src="picture4" /> </a>

$(".external").click(function(event) {
   // redirect user to your middle page and pass url as query string paratmeter
window.location.href = "yourexternalpage.aspx?redirecturl=" + $(this).href() ;


Options 2 - if you can't add css class to all external links.

Sample html

<a href="yourwebsitedomain.com">staying in site</a>
    <a href="two">going external</a>

    var a = document.getElementsByTagName('a');
    var b = a.length;

        a[b].onclick = function(){
                // redirect user to your middle page and pass url as query string paratmeter
window.location.href = "yourexternalpage.aspx?redirecturl=" + $(this).href() ;
                window.location.href=$(this).href() ;

Server side Just an high level idea...

  1. Create middle page which take querystring as redirect url.
  2. on the code behind page load, read querystring redirect user using response.redirect.

  3. All your hyperlink should have server side event which should trigger on click of link.

  4. in your event, you can use below logic

     if(domain of redirect url and current site is same){
    redirect user to redirect url
    redirect user to middle page and pass redirecturl as query string
  • I don't think so....above solution is with jquery..!! Commented Dec 15, 2016 at 10:15
  • then you need to do server side.... Commented Dec 15, 2016 at 10:22
  • How can I do for the URL's in the content page? Is it possible?
    – Ram
    Commented Dec 19, 2016 at 9:50
  • for content pages you can use option 2 in my answer...!! Commented Dec 19, 2016 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.