1

I have a textbox and a button in my ASP web page. I am using JavaScript to validate the trigger from both the textbox and button. Once the validation is okay, I am trying to redirect the user to a different page.

Here is my code:

HTML

<input style="background: url(images/find.png) no-repeat; padding-left:20px;"type="text" id="txtSearch" onkeyup="validChar(this);" onkeypress="checkKey(event)" name="txtSearch" />

<button class="locButton" id="btnsave" onclick="fncsave(event)">Search</button>

JS

<script type="text/javascript">
function validChar(e) {
    e.value = e.value.replace(/[^a-zA-Z0-9]/g, '');
    e.style.border = "2px inset #EBE9ED";
}

function fncsave(e) {
    //alert('test');
    var t = window.document.getElementById('txtSearch');
    if (t.value.length > 0) {
        alert(t.value);
        redirectPage(t.value);
        //document.cookie = 'postbackcookie=';
        //document.location.href = "www.mydomain.com/search.aspx?search_results.aspx?searchtext=" + t.value + "&folderid=0&searchfor=all&orderby=title&orderdirection=ascending";
        //return false;
    } else {
        e.preventDefault();
        t.style.border = "2px inset #CC0000";
        alert("Search Query is Blank...");
    }
}

function checkKey(e) {
    var t = window.document.getElementById('txtSearch');
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 13) { //Enter keycode
        if (t.value.length > 0) {
            alert('enter press and ' + t.value);
            document.cookie = 'postbackcookie=';
            document.location.href = "www.mydomain.com/search.aspx?searchtext=" + t.value + "&folderid=0&searchfor=all&orderby=title&orderdirection=ascending";
            return false;
        } else {
            e.preventDefault();
            t.style.border = "2px inset #CC0000";
            alert('enter press and 0');
        }
    }
}

function redirectPage(val) {
    document.cookie = 'postbackcookie=';
    document.location.href = "www.mydomain.com/search.aspx?search_results.aspx?searchtext=" + val + "&folderid=0&searchfor=all&orderby=title&orderdirection=ascending";
    return false;
}
</script>

What happens is when the button is pressed, the page refreshes and instead of going to the search_results page, it just reloads itself. Do I have to use a form?

By Default the aspx page has a <form id="form1" runat="server"></form> in the page but I am trying to avoid using the running it at server.

Si8
  • 9,141
  • 22
  • 109
  • 221

1 Answers1

2

document.location.href with a relative path is just going to change the last part of the url (ie the search).

To change the whole path, you should add a / to the beginning.

So:

window.location.href = '/searchtext=sadsd';

will make domain.com/some/url navigate to domain.com/newurl?searchtext=sadsd

To only replace the last part, you should add ../ to the beginning So:

window.location.href = '../searchtext=sadsd';

will make domain.com/some/url navigate to domain.com/newurl?searchtext=sadsd

Also, you should use window.location and not document.location, as seen here: What's the difference between window.location and document.location in JavaScript?

Community
  • 1
  • 1
Etai
  • 1,483
  • 1
  • 11
  • 15
  • I left out part of the code. The entire code is: `www.mydomain.com/search.aspx?blah&blah&blah`. `window.open(http://www.google.com);` works without any issue... – Si8 Jan 23 '14 at 21:09
  • If I use this code: `window.location.href = "http://www.google.com";` I can see the progressbar in the tab circling and also the `google.com` is showing but then instead of going there, the current page just refreshes. – Si8 Jan 23 '14 at 21:14
  • I don't understand how what you wrote relates to the problem you wrote about. here's a jsfiddle: http://jsfiddle.net/bqzh8/ The first search is with your code, the second search box is the version which navigates properly – Etai Jan 23 '14 at 21:14
  • Sorry. What I am trying to say is I changed the existing code to www.google.com to see if it actually redirects. It is redirecting but not loading it. I do have the edit you showed me but for some reason it's not working :/ – Si8 Jan 23 '14 at 21:16
  • just try the code in console here too, and you'll see... window.location.href = some_absolute_path will direct to it. window.location.href = some_relative_path will work as I explained in the answer above. – Etai Jan 23 '14 at 21:16
  • I am in the chat room waiting :) – Si8 Jan 23 '14 at 21:21