On a web page there is a textbox. I would like to pass the value from querystring and fill into the textbox.
I have a jQuery function successfully capture the querystring value. Here is the jQuery code I tried to fill into the textbox:
$(function() {
var keyword = getParameterByName('k');
alert(keyword);
$("#SearchBox input:text").eq(0).val(keyword);
});
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, " "));
}
Above code works fine. However, if I remove the line alert(keyword)
, the textbox will not be filled. If I move the alert(keyword)
to the end of function the textbox will not be filled, too.
Why is that? I cannot think of any reason an alert line will influence the outcome. Please give me some idea. Thank you!