0

I have a javascript to ask the user to confirm that they want to leave my website. This is how it looks:

  function confirmLeave()
  {
    if(confirm("Do you want to leave my webpage?")) {
      return true;
    } else {
      if(window.event) {
        window.event.returnValue = false;
      } else {
        e.preventDefault();
      }
      return false;
    }
  }

function initiate ()
{
    var links = document.getElementsByClassName("external-link");
    for (var i = 0; i < links.length; i++) {
        links[i].onclick = confirmLeave;
    }   

}
window.onload = initiate;

It works good in Firefox and Chrome but not in IE8. I know that document.getElementsByClassName doesn't work and I tried to remake my code using document.getElementsbyTakeName but haven't got it to work. This is what I came up with:

function confirmLeave()
      {
        if(confirm("Do you want to leave my webpage?")) {
          return true;
        } else {
          if(window.event) {
            window.event.returnValue = false;
          } else {
            e.preventDefault();
          }
          return false;
        }
      }

funciton initiate ()
{
    if(document.getElementsByClassName)
    {   
    var links = document.getElementsByClassName("external-link");
    for (var i = 0; i < links.length; i++) {
        links[i].onclick = confirmLeave;
    } 
    } else {
     var links = document.getElementsByTagName("external-link");
    for (var i = 0; i < links.length; i++) {
        links[i].onclick = confirmLeave;
    }
    }
}
window.onload = initiate;

The external-link is the class I'm using for the links that are to leave my webpage.

Benji
  • 615
  • 5
  • 11
  • 25

3 Answers3

2

Try using document.querySelectorAll(".external-link")

source

Community
  • 1
  • 1
DDK
  • 1,032
  • 18
  • 39
  • Just replacing document.getElementsByClass? – Benji Jan 30 '13 at 10:08
  • Yes. Although it takes a CSS selector as input, so `getElementsByClassName("external-link")` becomes `querySelectorAll(".external-link")`. – Olly Hodgson Jan 30 '13 at 10:12
  • @Benji, Yes. According to MSDN `W3C Selectors API provides two methods for using selectors to select objects: querySelector, which selects the first object that matches your criteria; and querySelectorAll, which returns all objects that match your criteria. You can select among the document objects or those that descend from a common container object.` – DDK Jan 30 '13 at 10:20
0

consider including jQuery library if that's a viable option for you. then you may write something like this:

$('.external-link').click(function() { confirmLeave() });
0

The problem here is that document.getElementsByTagName() takes an element as it's input, e.g. div, span or a. external-link is not an element, so it'll never return anything.

For IE8, you can use document.querySelectorAll(".external-link"). It takes a CSS selector as it's input.

If you need to support older browsers too, you could try using https://gist.github.com/2397759 which is a polyfill to make getElementsByClassName work on browsers that don't support it natively.

Olly Hodgson
  • 15,076
  • 3
  • 40
  • 60