0

I'm trying to prevent page closing when clicking on a external link used this JavaScript code. code is running in that other page.

if(parent.window.opener) parent.window.opener.location='//www.domain.com';

I have a lot of links, difficult removed or use window.onbeforeunload annoying.

Is there any code do this?

Thanks.

Harrttie
  • 1
  • 3
  • Take a look [here](http://stackoverflow.com/questions/3022069/javascript-get-anchor-href-on-click) – Kiril Dec 20 '15 at 14:20
  • I do not understand your first sentence. So a person clicks on a link to another domain and that JavaScript code above is running in that other page? How do the links play role in that code above? – epascarello Dec 20 '15 at 14:22

1 Answers1

0

Using this

    var domain = function(url) {
        return url.replace('http://','').replace('https://','').split('/')[0];
    };

    return domain(location.href) !== domain(url);
}

Use it here to prevent the default behavior of a link:

document.getElementsByTagName('a').addEventListener("click", function(e){
if(domain(e.target.href)){
e.preventDefault();
}
}, false);
Community
  • 1
  • 1
itamar
  • 3,837
  • 5
  • 35
  • 60