0

I have the following code, which works great except it also catches subdomains on the same domain. For example if on example.ca and I want to go to sub.example.ca it will not let me. I understand that the site is technically different but I do not need the warning for these pages.

Also on the site sub.example.ca (since it uses most of the same files) triggers for every internal link since the internal links are now sub.example.ca which is really annoying.

I could use the bypass variable but do not want to go through adding it to every menu and page I create. Laziness at it's best. haha.

$(document).ready(function() {
  $("a").click(function() {
    var href = this.href;
    var ourDomainRegex = /^https?:\/\/(www[.])?example[.]ca/;
    // those seeking flexibility, consider this: 
    // new RegExp('^https?:\\/\\/(www[.])?'  +  ourDomain)
    if (href.indexOf("http") === 0   &&   ! ourDomainRegex.test(href) )
    {
      if($(this).attr('rel')!='pass') {
          this.href = "/leave.php?p=" + href;
      }
    }
  });
});

How would I go about getting it to work for subdomains. Thanks!

Dawson Irvine
  • 322
  • 4
  • 19
  • `$(window).on("beforeunload", function(){return "wait dont leave!"})` [read the limitations](https://developer.mozilla.org/en-US/docs/Web/API/Window.onbeforeunload) – megawac Oct 20 '13 at 23:46
  • See http://stackoverflow.com/questions/14645011/window-onbeforeunload-and-window-onunload-is-not-working-in-firefox-safari-o – megawac Oct 20 '13 at 23:48

1 Answers1

0

You can make your regular expression permit any subdomains:

/^https?:\/\/([a-z0-9-]+[.])*example[.]ca/
Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
  • This still doesn't appear to work and still redirects to the warning when the link is to a sub-domain or if on the sub-domain site any internal link. :( – Dawson Irvine Oct 31 '13 at 04:42