Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can OnClick reference a .js file?

Status
Not open for further replies.

xencade

MIS
Jul 16, 2002
10
US
I have a form that I want to kick-off a .js file. I can get the form to kick-off a function withhin the .html, but I need it to be integrated into a "already written" .js file. Basically what I want this to do is give a user two options -

1. Either lookup weather by zip, in which I splice in the form input value to a pre-defined http: address or

2. Select pre-defined weather locations. But not both.

Here is the FORM:

<form name=&quot;zipsearch&quot; method=&quot;post&quot; action=&quot;&quot; enctype=&quot;multipart/form-data&quot;>
<font color=&quot;#000000&quot;>Enter Zip Code</font>
<input name=&quot;findzip&quot; maxlength=&quot;5&quot; size=&quot;5&quot; TYPE=&quot;text&quot;>
<input type=&quot;button&quot; value=&quot;GO&quot;
onClick=&quot;GO()&quot;>
</form>


Here is the GO function:

function GO()
{
var zipcodepars = document.zipsearch.findzip.value;
{
document.weathermagnet.src='// alert(&quot;You Chose &quot; + zipcodepars)
// document.write(zipcodepars)
// document.write(&quot;<a name=weathermagnetlink
this is the trouble area - using the zipcodepars variable in a pre-defined http: address
}
}

What I want to do is use the zip code and parse that zipcode into the http address, accept I need it to do this in the .js file within the GO function.
 
naming conventions would be helpful. what you mean by kick off? Execute?

Does the function GO() do something in particular? maybe you should consider naming it something that tells you what it really does. Gary Haran
 
GO is just the button name on the form. It collects a users zip code and then the onclick kicks off the GO function. Yes, execute would be a term used as well.
 
if in your page you have a <script src=&quot;myJS.js&quot;></script> and inside myJs.js you have the go function you should be able to execute it on the page. Gary Haran
 
Xutopia - that portion worked great - Thank you for the feedback.

I'm now faced with a dilemna like this:

I want to use the form value that I define a variable within a function (to make it simple) and splice in the form inputed value into a url.

For example, let's say the form asks for a zipcode. The variable name I give it in the GO function is zipcode as well. Now, I want to parse that variable into a pre-determined url (
I would want the 12345 in the url above to be dymanic depending on the input from the form.

Can I do this within a function of a .js file?
 
regarless of where you define your function you can make it do the same things. This means that a function defined in a js file or in the page itself does the same thing.

If you want to send a user to a location that depends on the values entered you can use location.href as well as the value of the document element.

here is a sample of how you could do it :

<script>
function getDefinition(word)
{
location.href = &quot; + escape(word); // escape makes sure no characters are invalid url chars. :)
}
</script>
<form name=&quot;myForm&quot;>
<input type=text name=word>
<input type=button value=search onclick=&quot;getDefinition(document.myForm.word.value)&quot;>
</form>

I hope this helps. Gary Haran
 
Some error checking with this kind of user input is always in order to make sure the user hasn't entered anything that will result in Page Not Found errors, which are ALWAYS the designer's fault and not due to typographical errors or other problems between the user's brain and fingers. :)# I'd first make sure the zip code information entered is only digits, with no other letters, punctuation, or other characters.
 
Yeah, I agree. Would that be in my html form code to do that sort of checking?

Thanks for your feedback, this forum is great. I am a native Wise/Winbatch programmer and am new to java and html still. With this type of feedback, I'll be up on my feet in no time..
 
Since you're expecting onlly numbers for U.S. zip codes, a simple:

if (isNaN(word))
{
alert('Digits 0 - 9 only!');
document.zipsearch.findzip.focus();
return;
}

at the beginning of the function that changes the URL will stop the page from changing location, alert the user to the error, and put the cursor back in the text box for them to try again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top