Intercepting Clicks: Pop external links

  1. Block external links
  2. Intercepting Clicks
  3. Dangerous links

Finally, we’re going to make this do what we planned to do all along: external links go into a new window (or tab), local links stay in the current window. We’re already set: we have the function doing something special if the link is an external link.

That something special is to block the link, but we don’t want to block it, we just want to open it in a different window.

We’ve been using window.alert without talking about it. The window is an object just like the document is an object. The document is the web page; the window is the browser window that contains the web page. One of the methods that the window has is the method to display an alert box. Another method is a method to open a new window.

Above “return false” in manageLinks, add:

window.open(url, "ExternalToReviews");

The if should now be:

if (linkIsExternal) {

window.open(url, "ExternalToReviews");

return false;

}

The window.open method accepts two parameters: the URL to go to, and the name of the window it will open in. If a window of that name already exists, window.open uses it; otherwise, it creates a new window with that name. This helps keep from filling the visitor’s screen with browser windows.

  1. Block external links
  2. Intercepting Clicks
  3. Dangerous links