0

not sure if this should be done in javascript or something else. Basically, I have a little quiz type thing where all questions are on a single page. I have an ajax function which checks what question it is, and if it is the last question, it will redirect the user to a new page e.g.

if(totalScore <= 10) {
    $.ajax({
        method: "POST",
        url: "php/handleData.php",
        data: { answers: ansArray, page: window.location.href, pt: "aa" }
    }).done(function( response ) {
        window.location.replace("page2.html" + '?le=' + le + '&ch=' + ch);
    }).fail(function( jqXHR, textStatus  ) {
        console.log( "Request failed: " + textStatus );
        window.location.replace("page2.html" + '?le=' + le + '&ch=' + ch);
    });
    return false;
}

As you can see, the redirect also includes some additional parameters. Therefore, when I am redirected, I am on a page like

page2.html?le=1&ch=2

Now I dont necessarily need these params in this url, but it is the only way I could think about getting them over to page2. Page 2 has a button on it like

 <a href="www.link1.com>" target="_blank" class="btn btn-primary" role="button">Visit link1.com ></a>

It is in this href where I need the params le and ch to be injected.

How would I go about doing this?

Thanks

katie hudson
  • 2,765
  • 13
  • 50
  • 93

1 Answers1

0

With JQuery on Page2.html you can do that:

$(document).ready(function(){

    function getParameterByName(name) {
        name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
        return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    }

    var le_val = getParameterByName("le");
    var ch_val = getParameterByName("ch");

    $("a.btn.btn-primary").attr("href","www.link1.com?le="+le_val+"&ch="+ch_val);
});
vijayP
  • 11,432
  • 5
  • 25
  • 40