user8774850
user8774850

Reputation:

How to write JS function in a .js file

I wrote a function in Html and it works well. enter image description here But My teacher said we need separate the code out of HTML file. So I need to implement this code in a .js file. Can anyone tell me how to do that? I think to create a function in JS like this but it not working.enter image description here

Thanks for any help!

 <script>
$(document).ready(function() {
$("#down").on('click',function(event) {
$('html,div-b').animate({scrollTop: document.body.scrollHeight 
-1100},"slow");

 });
});

Upvotes: 2

Views: 3678

Answers (4)

Gowtham
Gowtham

Reputation: 1597

You have use html file like this

<html>
<body>

<script src="demo.js">
</script>

</body>
</html>

and keep the js file like this demo.js file

function test(){
<!---your code here-->
}

Upvotes: 2

Tejendra
Tejendra

Reputation: 159

Write the code as it is in file and save it with .js extension and link it in html under head tag as follows

<script src="myscripts.js"></script>

Upvotes: 1

Saifer
Saifer

Reputation: 380

Put all the js code in a .js file, then put the code below in the html page which will call it, inside the head or the body.

<script src="myScript.js"></script>

Upvotes: 1

Rahul
Rahul

Reputation: 482

copy the code inside the <'script> tag and paste in a separate .js file . This is how it works

Upvotes: 2

Related Questions