jQuery open external link in new window

/ Published in: JavaScript
Save to your folder(s)

Two different ways to open an external link (ones that start in http) in a new window. I know this isn't a great thing to do, but some clients _need_ to have it.

Copy this code and paste it in your HTML
  1. //Grab the href, open it in a window and cancel the click action
  2. $("a[href^='http']").click(function(){window.open(this.href); return false;});
  3. //Add target = blant to the external link
  4. $("a[href^='http']").attr('target','_blank');
  5. //Similar to the first, only using your domain name
  6. $("a:not([href*='yourdomainnamehere.com'])").click(function(){
  7. window.open(this.href);
  8. return false;
  9. }).attr("title", "Opens in a new window");

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.