Forum Moderators: open

Message Too Old, No Replies

Search form submit, URL rewriting & how to submit for non-support

Submitting javascript form the same regardless of support

         

Seidl

3:58 pm on Apr 4, 2009 (gmt 0)

10+ Year Member



I have a search form that, when submitted, returns a clean URL (i.e. /KEYWORD/search). The problem is that I can't get the search form to work when JS is not supported. I tried:

<form method="get" action="search" onsubmit="GoToURL(this)">

but it doesn't work. However, using onclick with the submit button does work. Here is the function that I am using:

<script type="text/javascript" language="JavaScript">
<!--
function GoToURL(j) {
window.status=('Searching...')
var hyphen = "-";
var firstURL = document.searchform.searchfield.value;
var cleanURL;
cleanURL = firstURL.replace(/ /g, hyphen);

if (cleanURL == "")
{
var location=("http://www.domain.com/default-search-page");
this.location.href = location;
}
else
{
var location=("http://www.domain.com/" + cleanURL + "/search");
this.location.href = location;
window.status=('Searching for ' + cleanURL + '...');
}
}
//-->
</script>

rocknbil

6:54 pm on Apr 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard Seidl.

If you wish to have non-JS support, you'll need to do two things. The first, change your Javascript routine so it returns false. This allows the form to submit from Javascript if it's enabled, and from the form if it's not enabled. Returning false tells the browser not to perform it's normal action, which stops it from submitting.

.....
return false;
} // End goToURL()
</script>

Second,
<form method="get" action="search" onsubmit="GoToURL(this)">

I am presuming "search" is a mod_rewrite to a script of some kind that does a search. Simply duplicate the actions in your Javascript in this script.

You can manage this in many ways, the two most obvious are to allow the search script to just output based on the input term, or to print a Location: header just like you're doing in your Javascript.

Minor points:
<script type="text/javascript" language="JavaScript">

no longer needs the language attribute, and
<!--
//-->

Is no longer required,

var firstURL = document.searchform.searchfield.value;

Assign id's to your form objects and use document.getElementById('object-id') to manipulate your documents.

Seidl

11:16 pm on Apr 4, 2009 (gmt 0)

10+ Year Member



Thanks for the welcome. :) I implemented return false and have set the form's action to an intermediary page that outputs the search term into the URL; I should have thought of that! I use VBScript (classic asp) so you are correct that the URLs are rewritten.

While I'm at it, I was wondering if anyone knew a way to include external JS files that also have VBScript in them? I haven't been successful so far. Maybe I need a new thread for that.