0

I created a query string and appended it to the URL like so:

// HTML from index.html
<input type="button" value="Yes" onclick="redirect(this);" id="YES" class="btn btn-primary btn-lg">
<input type="button" value="No" onclick="redirect(this);" id="NO" class="btn btn-primary btn-lg">
// JS on index.html
function redirect(e){

    var mapPg = "map.html";
    var qString = "?choice=";
    location.href = mapPg + qString + e.id;

}

Beginning on the map.html page, I want to take that query string and carry it through to every URL, except the index.html page.

When the user clicks on price.html pg, it'll use the query string to call a function that will pull in the data for the "yes" answer. Same thing goes for the "no" answer.

I can't figure out how to append the query string to the URL of every pg on click.

The nav is a JavaScript include that's been pulled in. I've added a class of qString to all the<a> in the nav in case it would be easier to target them.

1
  • 1
    You can't set a cookie or a sessionStorage or localStorage? Commented Apr 24, 2015 at 21:34

4 Answers 4

1

Is jQuery an option? If so...

<script type="text/javascript">
$(document).ready(function() {
    $("a").prop("href", function() { return this.href + location.search; })
});
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

This answer somewhat worked, but it disabled all <a> tags. I used this answer and combined it with @Barmar 's variable.
0

You can add this to each page:

window.addEventHandler("load", function() {
    var match = location.search.match(/\bchoice=([^&]*)/);
    if (!match) {
        return;
    }
    var choice = match[1];
    var navlinks = document.getElementsByClassName("qString");
    for (var i = 0; i < navlinks.length; i++) {
        if (navlinks[i].search == '') {
            navlinks[i].search = '?choice=' + choice;
        } else {
            navlinks[i].search += '&choice=' + choice;
        }
    }
}

1 Comment

This one didn't work for some reason. I really appreciate your help. I used a combo of your variable and @ShawnJacobson's answer.
0
location.href = mapPg + qString + e.id;

the code here needs to be changed. e.getAttribute("id")

Comments

0

Thank you @ShawnJacobson and @Barmar . Although the individual answers didn't work properly, I combined a bit of both to arrive at a solution that works.

var navlinks = document.getElementsByClassName("qString");
    $(navlinks).prop("href", function() { return this.href + location.search; })

Shawn's answer somewhat worked but it disabled all the <a> tags. Barmar's answer didn't work but after seeing the variable var navlinks it gave me an idea of what to do.

I really appreciate your help at arriving at an answer.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.