0

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!

Mark
  • 283
  • 3
  • 22
  • share the method definition for `getParameterByName` – Milind Anantwar Jan 28 '15 at 10:33
  • Is the `#SearchBox input:text` element being loaded by AJAX, or otherwise appended to the DOM after the page is loaded? Does `getParameterByName()` use asynchronous functions? – Rory McCrossan Jan 28 '15 at 10:33
  • function GetQueryStringParams(sParam) { var sPageURL = window.location.search.substring(1); var sURLVariables = sPageURL.split('&'); for (var i = 0; i < sURLVariables.length; i++) { var sParameterName = sURLVariables[i].split('='); if (sParameterName[0] == sParam) { return sParameterName[1]; } } }​ – Mark Jan 28 '15 at 10:34
  • Rory, I am not sure. I am editing SharePoint's search center default page. – Mark Jan 28 '15 at 10:36
  • Your function is named `GetQueryStringParams` yet you call `getParameterByName` ...? – Rory McCrossan Jan 28 '15 at 10:36
  • I found this http://stackoverflow.com/questions/17964629/javascript-function-not-working-without-alert very close to my problem. But I don't know how to apply on my code. – Mark Jan 29 '15 at 01:46

0 Answers0