2

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");

 });
});

5
  • 2
    You don't need to create a function, just move all your code into this file. Commented Oct 25, 2017 at 10:44
  • @Walk is right, just move the code to the separate JS file and then link to that JS file in the HTML file. Commented Oct 25, 2017 at 10:45
  • Please add your code as text in your psot, it's easier to work with than images Commented Oct 25, 2017 at 10:46
  • Hey guys. Thanks for your help. But actually, There already have some function my teammate wrote in the JS file. I tried to copy the code to top of all javascript, but it can't work Commented Oct 25, 2017 at 10:47
  • @Wndrr oh, I forgot it. Thanks, will do Commented Oct 25, 2017 at 10:47

4 Answers 4

2

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-->
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your help. We already have a js file called 'myJS' and linked with the HTML. We also have some different function in 'myJS' file. I tried to create a test function with the code but it seems can't work
Check whether both html and js are in same path,if js is in different path mention the correct path
yeah, the path is right. and all other function works well. only the scroll down function I just wrote can't work.
check whether it is throwing any error in console?? put an alert and check whether the alert is firing
Yeah, I think my teammate found how to fix it. thanks for the help again!
|
2

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

1 Comment

Thanks for your help. We already have a js file called 'myJS' and linked with the HTML. We also have some different function in 'myJS' file. I tried to create a test function with the code but it seems can't work
1

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>

2 Comments

Thanks for your help. We already have a js file called 'myJS' and linked with the HTML. We also have some different function in 'myJS' file. I tried to create a test function with the code but it seems can't work
A function should be called, or in the js file itself, or with some other js code, something like jsfunction()
1

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>

2 Comments

Thanks for your help. We already have a js file called 'myJS' and linked with the HTML. We also have some different function in 'myJS' file. I tried to create a test function with the code but it seems can't work
If you create a function as we create in javascript(ex. function scrolldown(){}) , then you have to call them from html. otherwise it wont work because javascript does not use selection framework which jquery use.So dont write your code in scrolldown function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.