+ 1

How to use external JavaScript? What should I type in .js file so it will load in html file?

I saved both files in same folder but JavaScript in not loading in my html file.

20th Jul 2020, 4:39 AM
Deepak Sanjaykumar More
Deepak Sanjaykumar More - avatar
22 ответов
+ 3
I did, it worked... Html: <!doctype html> <html> <head> <title></title> </head> <body> <script type="text/javascript" src="code.js"></script> </body> </html> Js: (code.js) document.write("hello world"); It was ok...
20th Jul 2020, 5:37 PM
EmmanueLZ.
EmmanueLZ. - avatar
+ 2
But it worked for me with or without it, so it might be something else... did you add a link tag within your head tags ? try with another browser also...
20th Jul 2020, 5:41 PM
EmmanueLZ.
EmmanueLZ. - avatar
+ 2
Ok, nice👍🏻. Have a good day 🌞.
20th Jul 2020, 5:51 PM
EmmanueLZ.
EmmanueLZ. - avatar
+ 2
Use onload event ....
21st Jul 2020, 5:38 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
<script src="file name.is" type="text/Javascript"></script>
21st Jul 2020, 1:07 PM
The CodeGeek
The CodeGeek - avatar
+ 2
Deepak Sanjaykumar More window.onload = function(){ //Write your code here }
21st Jul 2020, 9:32 PM
Nada Ashraf
Nada Ashraf - avatar
+ 1
Deepak Sanjaykumar More can you show me your code? Can you tell me names of your file.
20th Jul 2020, 8:49 AM
🇮🇳Vivek🇮🇳
🇮🇳Vivek🇮🇳 - avatar
+ 1
EmmanueLZ. Bro, he already mentioned that he has written script tag in body tag.
20th Jul 2020, 4:11 PM
🇮🇳Vivek🇮🇳
🇮🇳Vivek🇮🇳 - avatar
+ 1
🇮🇳Vivek🇮🇳 thanks for pointing it, I read bad about his answer later,my bad. So... Deepak Sanjaykumar More , if you used script within body tags, is it placed by the end, before the closing body tag ? Also try to add as an attribute <script type="text/javascript" src="code.js"></script>
20th Jul 2020, 4:36 PM
EmmanueLZ.
EmmanueLZ. - avatar
+ 1
Just tried and it worked for me, with or without the type="text/javascript". Would you post the whole html part ?
20th Jul 2020, 4:53 PM
EmmanueLZ.
EmmanueLZ. - avatar
+ 1
Check the console in the Dev tools of your browser to see if it reports an error and what is it.
20th Jul 2020, 5:46 PM
EmmanueLZ.
EmmanueLZ. - avatar
+ 1
EmmanueLZ. I found my error I didn't written !doctype in my first html code.
20th Jul 2020, 5:50 PM
Deepak Sanjaykumar More
Deepak Sanjaykumar More - avatar
0
You don't need to do anything in js file. You use script tag in HTML. <script src="./your-file-name.js"> </script>
20th Jul 2020, 5:09 AM
🇮🇳Vivek🇮🇳
🇮🇳Vivek🇮🇳 - avatar
0
Vivek I did as you said but that js function is not loading in my browser. So can you give me an example of js coding in .js file
20th Jul 2020, 8:07 AM
Deepak Sanjaykumar More
Deepak Sanjaykumar More - avatar
0
Vivek my code in code.js file is document.write("Hello World"); & in html in body tag is <script src="code.js"></script> but in browser there is no output of Hello World. Help me to solve this problem :)
20th Jul 2020, 10:03 AM
Deepak Sanjaykumar More
Deepak Sanjaykumar More - avatar
0
Why src is in uppercase? SRC must be in lowercase
20th Jul 2020, 1:31 PM
🇮🇳Vivek🇮🇳
🇮🇳Vivek🇮🇳 - avatar
0
That SRC is in lower case this Google auto correct changed it uppercase in mobile. I'm practicing my codes on laptop.
20th Jul 2020, 1:41 PM
Deepak Sanjaykumar More
Deepak Sanjaykumar More - avatar
0
Deepak Sanjaykumar More It should work. Calviղ can you help?
20th Jul 2020, 4:13 PM
🇮🇳Vivek🇮🇳
🇮🇳Vivek🇮🇳 - avatar
0
Yes I used script tag just before end of body tag. You should try this simple one line document.write of java as an external source in html. My other external code(alert) of js is worked but this document.write is not working.
20th Jul 2020, 4:43 PM
Deepak Sanjaykumar More
Deepak Sanjaykumar More - avatar
0
EmmanueLZ. Type document.write("Hello World!"); in js and run it in html.
20th Jul 2020, 5:29 PM
Deepak Sanjaykumar More
Deepak Sanjaykumar More - avatar