JQuery not working without alert()

JQuery not working without alert()

Dear all,

I am updating a SharePoint webpage. The page belong to search center. So there is a google-like textbox in the middle of page. My script is try to get the querystring and fill in the textbox. Here is the code:

  1. $(function() {
        var keyword = getParameterByName('k');
        alert(keyword);
        $("#SearchBox input:text").eq(0).val(keyword);
    });
  2. 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 is working fine. However if I remove the line "alert(keyword);". The code will not work. There is no error, just the textbox not being filled. I guess it is because textbox is not fully loaded unless an alert box is called. How can I work around it?

Thanks for any input!