+ 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.
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...
+ 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...
+ 2
Ok, nice👍🏻.
Have a good day 🌞.
+ 2
Use onload event ....
+ 2
<script src="file name.is" type="text/Javascript"></script>
+ 2
Deepak Sanjaykumar More
window.onload = function(){
//Write your code here
}
+ 1
Deepak Sanjaykumar More can you show me your code? Can you tell me names of your file.
+ 1
EmmanueLZ. Bro, he already mentioned that he has written script tag in body tag.
+ 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>
+ 1
Just tried and it worked for me, with or without the type="text/javascript".
Would you post the whole html part ?
+ 1
Check the console in the Dev tools of your browser to see if it reports an error and what is it.
+ 1
EmmanueLZ. I found my error I didn't written !doctype in my first html code.
0
You don't need to do anything in js file. You use script tag in HTML.
<script src="./your-file-name.js">
</script>
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
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 :)
0
Why src is in uppercase? SRC must be in lowercase
0
That SRC is in lower case this Google auto correct changed it uppercase in mobile. I'm practicing my codes on laptop.
0
Deepak Sanjaykumar More It should work. Calviղ can you help?
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.
0
EmmanueLZ. Type document.write("Hello World!"); in js and run it in html.