PDA

View Full Version : Append to an href using js/ jQuery



tonybabb
01-30-2014, 08:59 AM
Good morning,

I'm trying to append a couple of parameters to an href and can't get it to work - the parameters just don't get appended.

Here's the html showing a button the user clicks which launches a js script that validates the users responses to several questions. If the responses are valid the js then updates a couple of variables - blapp and tspapp. These are then appended to an href and the link is clicked by js. Everything seems to be working except that the parameters are not appended to the href.

I tried two different ways, one was to use an embedded script with document.write, that didn't work and the other was to use jQuery append and that didn't work either...sigh.

Here's the html showing the button and immediately below it the document.write code that I commented out and below that the link that I tried to append using jQuery.


<input id="continue" type="button" value=" Continue " onclick="checkAnswers();" />
<!-- <a id="appLink" href="http://www.hollisterairshow.com/rally/vendorApp.php?<script>document.write("blapp="+blapp+"&tspapp="+tspapp)</script>"></a> -->
<a id="appLink" href="http://www.hollisterairshow.com/rally/vendorApp.php?"></a>

Here's the js that validates responses and updates the values of the two parameters


<script>
function checkAnswers() {
if(selling === "omitted" || tsp === "omitted" || (selling === "yes" && curbl === "omitted"))
{
dispMessage();
return;
};
alert("All OK so set up link to vendor app");
if (selling === "yes" && curbl ==="no") {
blapp = "yes";
}
else {
blapp = "no";
};
if (tsp === "yes") {
tspapp = "yes";
}
else {
tspapp = "no";
}
alert("blapp="+blapp+" tspapp="+tspapp);
var params = "blapp="+blapp+"&tspapp="+tspapp;
alert("params = " + params);
$( "#appLink" ).append(params);
document.getElementById('appLink').click();
}
function dispMessage() {
alert("Please check your answers and press <Continue> again");
return;
} // /checkAnswers
</script>

The complete page can be seen here http://www.hollisterairshow.com/rally
Thanks for any assistance you can provide.

Tony

jscheuer1
01-30-2014, 09:19 AM
Using document.write after page load can have unpredictable consequences. And that's not how append works. And because only a few browsers support it, you shouldn't be changing and clicking links via javascript in order to change the page. Rather, just use javascript directly to change the page. So here:


$( "#appLink" ).append(params);
document.getElementById('appLink').click();

I would do instead:


window.location.href = 'http://www.hollisterairshow.com/rally/vendorApp.php?' + params;

tonybabb
01-30-2014, 01:59 PM
Thanks John, you make this look easy. I must have spent several hours trying to get it to work....sigh.

Thanks again.

Tony