Time for action — opening a link in a new window

  1. We'll get started with our basic HTML file and associated files and folders that we created in Chapter 1, Designer, Meet jQuery. Inside the<body> of the HTML document, we'll add some links as follows:
    <h1>Opening Links in a New Window</h1>
    <p>This link will open in a new window: <a href ="http://packtpub.com">New Window!</a></p>
    <p>This link will not: <a href ="http://packtpub.com">Same Window!</a></p>
    

    This is just a heading and two simple paragraphs, each with a link — one that should open in a new window and one that won't.

  2. We need some way to select the link that should open in a new window. This is similar to the situation we would have if we wanted to style one of the links differently from the other with CSS.

    If we were using CSS, we could assign the link an ID or a class. An ID would be pretty limiting, as an ID must be unique on a page — it would only apply to this one particular link. A class would let us style any link that opens in a new window, so that's what we're going to use. Add a class to the link that should open in a new window as follows:

    <a href="http://packtpub.com" class="new-window">New Window!</a>
    
  3. Now we can use this class name for both CSS styling and to make the link open in a new window with jQuery. It's a great idea to add an icon to this link you can add some padding to the left or right side of the link and then add a background image to the link. Open up the empty styles.css file inside your styles folder and add a bit of CSS as follows:
    a.new-window {
    padding-right: 18px;
    background: url('../images/new-window-icon.png') 100% 50% no-repeat;
    
  4. Next up, we'll open up the scripts.js file inside our scripts folder, and outside of our document ready statement we'll start off by writing our function to get our new-window links and make them open in a new window. Start off by declaring a new function:
    $(document).ready(function(){
    });
    function externalLinks() {
    }
    

    Here we've created a new function and named it externalLinks as that's a name that makes sense for opening links in new windows. It's always helpful to give your JavaScript functions and variables names that will help you remember what they do.

  5. Next, we'll use jQuery to select all the links with the class new-window. We'll take advantage of jQuery's CSS selectors to select those links just like we did when we styled them with CSS.
    function externalLinks() {
    $('a.new-window'),
    
    }
    
  6. We've used the $ shortcut for the jQuery function and passed the CSS selector to the function. It's important to remember to wrap the CSS selector in either single quotes or double quotes. We don't want the link to open a new window until the user clicks on the link, so our next step is to tell the link to run a function when it's clicked on. jQuery makes this very easy. We can use the bind() method provided by jQuery to bind a function to the link that will be called when the link is clicked. That will look like this:
    function externalLinks() {
    $('a.new-window').bind('click', function() {
    });
    }
    

    This bit of code binds a function to our link — when our link is clicked, any code we write inside this new function will be called. But so far, our function is empty and doesn't actually do anything.

  7. What we need to do next is get the location the link is sending us to:
    function externalLinks() {
    $('a.new-window').bind('click', function() {
    var location = $(this).attr('href'),
    });
    }
    

    Let's examine this new line of code one bit at a time. First, we've declared a new variable named location. As you remember, a variable is just a container. So we've got a new empty container, so now let's look at what we've put inside our container.

    $(this) is the jQuery way of referring to the jQuery object that we're currently working with. In this case, we're selecting all links with a class of new-window and we've attached this function to be called whenever a site visitor clicks the link. When a site visitor clicks a link, we want to examine the link that was clicked to get the location of where the link is going. A simple and quick way of referring to the current link is to use $(this).

    Next we use the attr() method to get an attribute of the link. The location where a link is heading is contained in the href attribute, so we pass href to the attr() method.

    So our container that we've named location now contains the URL where the link is pointing, or in this particular case, http://packtpub.com.

  8. Now that we know where we want to go, all we have to do is open that location in a new window. Opening a new window in JavaScript is simple and straightforward:
    function externalLinks() {
    $('a.new-window').bind('click', function() {
    var location = $(this).attr('href'),
    window.open(location);
    });
    }
    

    window is a global object in JavaScript that is always available to us. The window object has an open() method, and we just have to pass a location to that method so that the browser knows what location to open in a new window.

  9. Now, if you open this HTML page in a browser and try clicking the links, you might be disappointed to see that our link does not open in a new window. It's like our JavaScript isn't even on the page at all. We've written a very nice function, but it's not working. That's because functions don't do anything until we tell them to. Telling a function to do its thing in JavaScript speak is 'calling the function'.

    We would like this function to fire up, find all the links with the class new-window, and bind our new window function to them as soon as the page is loaded in the browser window. That way, our links that should open in new windows will be ready to fire off a new window as soon as our site visitor clicks on one of them.

    We just have to add a line inside our document ready statement to call our function:

    $(document).ready(function(){
    externalLinks();
    });
    function externalLinks() {
    $('a.new-window').bind('click', function() {
    var location = $(this).attr('href'),
    window.open(location);
    });
    }
    

    This new bit of code will call our externalLinks function as soon as the page loads up in the browser.

  10. There's just one thing left to do. Right now if you load the page in a browser and click on a link, you'll find that the link does indeed open in a new window, but it also opens in the current window — so we end up with our new page loaded into two different windows. Not exactly what we had in mind. What we need to do is cancel the default behavior of the link — we've already taken care of opening the location in a new window, so now we need to tell the browser that it can take a break and it doesn't need to do anything when the site visitor clicks on the link. So let's add a parameter to our function and a line of code to cancel the default link behavior.
    function externalLinks() {
    $('a.new-window').bind('click', function(e) {
    var location = $(this).attr('href'),
    window.open(location);
    e.preventDefault();
    });
    }
    

    You'll notice that the function we've attached to the click action on the link now has an e inside the parentheses. This is a parameter that we're passing to this function. In this case e represents the click event of the link.

    The line of code we add to the function is:

    e.preventDefault();
    

    This tells the browser to stop the default behavior of the link. If you reload the page in your browser and click on the link, you'll see that it correctly opens the destination page in a new window, and it no longer opens the link in the current window:

    Time for action — opening a link in a new window
  11. Now, what do you think will happen if we have a second link on the page that should open in a new window? Let's go back to the<body> of the document and add a second link that should open in a new window. After the other links, add a new paragraph and link to a new page:
    <p>This paragraph will open in a new window too: <a href="http://nataliemac.com" class="new-window">New Window!</a></p>
    

    Be sure to add the new-window class to your link.

Now, when you refresh the page in the browser, the new link appears on the page. Try clicking it and you'll see that it opens in a new window too, just like the other new-window link.

What just happened?

We added a CSS class name to the links that we wanted to open in a new window. Now, any link we create on our page with the new-window class will open in a new window but how does JavaScript know which page to open in a new window when there are multiple links?

The answer lies in our externalLinks function. We selected all links with the new-window class and bound a function to fire when those links were clicked. Inside that function, we captured the link's location. This function doesn't run until a link is clicked. Until then, it's just sitting on the sidelines, waiting to be called into action. When a link with the new-window class is clicked, our function springs into action, capturing the location of that specific link and opening up a new window pointed at that link's location.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.16.184