简体   繁体   中英

how to embed js script into html

So I was trying to make an online calculator and made a js script my question is how can i add this script to my html i tried using script but it didn't do anything

 function motion1() { var right=0; var initvel = calc.initvel.value; var finalvel = calc.finalvel.value; var time = calc.time.value; var accl = calc.accl.value; var dist = calc.dist.value; var complete="you have entered more than 3 values. Please leave two text box empty. Other two text box will be calculated based on that"; var incomplete="Sorry! incomplete information"; var validinput="You have not entered numeric values!!"; if (isNaN(calc.initvel.value)) return alert(validinput); if (isNaN(calc.finalvel.value)) return alert(validinput); if (isNaN(calc.time.value)) return alert(validinput); if (isNaN(calc.accl.value)) return alert(validinput); if (isNaN(calc.dist.value)) return alert(validinput); if(initvel!="" && finalvel!="" && time!="" && accl !="" ) return alert(complete); if(initvel!="" && finalvel!="" && time!="" && accl !="" && dist !="" ) return alert(complete); if(initvel!="" && finalvel!="" && time!="" ){ accl=(finalvel-initvel)/time; calc.dist.value= initvel*time + .5*accl*time*time; calc.accl.value=accl right++;} if(initvel!="" && finalvel!="" && accl!="" ){ time=(finalvel-initvel)/accl; calc.dist.value= initvel*time + .5*accl*time*time; calc.time.value=time right++;} if(initvel!="" && time!="" && accl!="" ){ initvel=parseFloat(initvel); calc.finalvel.value=initvel + (accl*time); calc.dist.value= initvel*time + .5*accl*time*time; right++;} if(finalvel!="" && time!="" && accl!="" ){ initvel=finalvel - (accl*time); calc.dist.value= initvel*time + .5*accl*time*time; calc.initvel.value=initvel right++;} if(dist!="" && time!="" && accl!="" ){ initvel=(dist - .5*accl*time*time )/time; calc.finalvel.value= initvel*time + accl*time; calc.initvel.value=initvel right++;} if(dist!="" && initvel!="" && accl!="" ){ finalvel=Math.pow(initvel*initvel + 2*dist*accl,.5); calc.time.value= (finalvel - initvel)/accl; calc.finalvel.value=finalvel right++;} if(dist!="" && initvel!="" && finalvel!="" ){ accl=(finalvel*finalvel - initvel*initvel)/2/dist; calc.time.value= (finalvel - initvel)/accl; calc.accl.value=accl right++;} if(dist!="" && finalvel!="" && accl!="" ){ initvel=Math.pow(finalvel*finalvel - 2*dist*accl,.5); calc.time.value= (finalvel - initvel)/accl; calc.initvel.value=initvel right++;} if(!right) alert(incomplete); // Less than the required fields entered! }

it is newton kinematics eq calculator if it is any helpful it should look like this enter image description here

so it would ask me for the value and it should be able to solve the other fields

The issue you made is... that you don't call the script. Currently you only have declared an JS-Function named motion1

To call it, only write "motion();" below your closed curly-bracket after the function.

BTW: Even if you call your Script, it won't work, because it has errors inside... if there are no global variable calc in your file. If your pasted script is only a part of the page and you've still have such an calc Object in your file, ignore this BTW-Comment ;)

Yes javascript belongs inbetween <script> </script> tags.

And then you bind actions to html elements like input boxes.

I would recommend starting with the basics and looking at some tutorials :)

https://www.w3schools.com/jsref/prop_text_value.asp

the better approach here you can download some calculator sample code from GitHub or any other platform and start debugging and modifying that code and try to add some more features to that calculator. Best of luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM