Si8
Si8

Reputation: 9225

Location.href is just reloading the current page

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.

Upvotes: 1

Views: 1536

Answers (1)

Etai
Etai

Reputation: 1503

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?

Upvotes: 2

Related Questions