Forum Moderators: open
<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>
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.
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.