Search code examples
javascripthref

Change all a hrefs with javascript?


How could I change all hrefs with custom URL on page load?

Sorry, I just have no clue how I could do that. Don't realy know any js.

Thanks to anyone who can help me. Realy appreciate it.


Solution

  • You can use this:

    // Execute this when the document is ready.
    window.onload = function () {
      // Get all the `<a>` tags.
      var all_a = document.querySelectorAll("a");
      // Loop through all the `<a>` tags.
      for (i = 0; i < all_a.length; i++)
        // Change the `href`:
        all_a[i].setAttribute("href", "whatever_you_want");
    }