Question:
how to run javascript rather than just displaying it?
Christopher
2014-02-19 12:58:07 UTC
I'm trying to run a javascript program. I am supposed to use an html page and a separate .js file which are retirement.html and retirement.js but when I try to submit the information from the html input form, it doesn't run the .js, it displays the code instead. I have the in the head of my retirement.html code and I have the form action as "retirement.js" and the method="post" (not sure if method could be the problem or not but in case it is...) anyway when I click the submit it only displays the code of the retirement.js file, it doesn't actually run any of the code. What's wrong? and I'll be happy to give you more info if this isn't enough.
Four answers:
KoryP
2014-02-19 13:19:30 UTC
The action attribute of the form tells the browser where to go when the form is submitted; typically a results page. You don't want to point it at the js file.



When you include the js via a script tag, the contents of the file is loaded and executed and any functions you create will be available to be run by other code.



What you'll need to do is write a function that gets triggered when the form is submitted. This can be accomplished via the onSubmit attribute of the form tag... There are better ways, but that will do.



Here's a quick example that you can play with:

http://www.w3schools.com/js/js_form_validation.asp (click Try it yourself)



If you can provide some sample code and a quick description of what you're trying to accomplish, I can provide more details.





EDIT:



Ok, not sure if I did the calculations correctly. Please double check. But here's a quick example:

http://jsfiddle.net/cDpLc/

You'd save the html into retirement.html and the javascript code into retirement.js. The html needs to have the and tags added with a

Let me know if you have any questions.
David W
2014-02-19 13:06:19 UTC
the "action" attribute of the form tag is where the form will submit to, or what function you want to run...you do not want to submit your page to a javascript page. Is there a second html page it's supposed to submit to? Or...is it supposed to submit to the same page (you can leave out the action parameter if so)....or is it supposed to execute a js function?? (if so, the action parameter should be the javascript function)



What is the javascript supposed to do?



Right now it's displaying your javascript as a page because you are telling your form to navigate (post) to that page....so the browser is just rendering what is in the .js file.
?
2014-02-19 16:36:41 UTC
s are meant to be sent to server-side scripts written in PHP or another server-side language.

JavaScript runs in the browser (client-side), not on the server, and to process a form with javascript, like KoryP explained, you basically need to intercept the default behavior using onsubmit and do something else instead (namely run JavaScript code).
anonymous
2014-02-19 13:06:33 UTC
You should be opening it up in a HTTP browser or Internet browser.



In the include the


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...