Search code examples
javascriptjqueryhtmlhref

Trying to combine href attribute and location.search


I would like to add a search query result to all subsequent page URL's that a visitor clicks on my site. However when a person on my page called "directions.html" clicks on the "About Us" link it comes up as:

http://justanexample.com/directions.html?guest#

The problem is that the link is going to directions.html and not about.html? Does anyone know what is causing that?

Javascript

$("#aboutHtml").attr("href", "about.html" + location.search);

HTML

<a id="aboutHTML" href="#">About Us</a>

Solution

  • Make sure your jQuery code runs after the links load.

    $(document).ready(function() {
      $("#aboutHTML").attr("href", "about.html" + location.search);
    });