This is a simple practical but I'm not sure what I'm doing wrong.
I have the following code in my external javascript file:
function myname(){
document.write("Monique");
}
function weleW(name){
document.write("Wele " + name + "!");
}
function weleR(name){
return "Wele " name "!";
}
I added this <script>
tag to link to my html file:
<script src="script.js" type="text/javascript"></script>
I tried calling the functions in html using:
<script> myname(); </script>
<script> weleW(Monique); </script>
<script> weleR(Monique); </script>
when I wrote the function in html it worked, but in the external file nothing happens.
This is a simple practical but I'm not sure what I'm doing wrong.
I have the following code in my external javascript file:
function myname(){
document.write("Monique");
}
function weleW(name){
document.write("Wele " + name + "!");
}
function weleR(name){
return "Wele " name "!";
}
I added this <script>
tag to link to my html file:
<script src="script.js" type="text/javascript"></script>
I tried calling the functions in html using:
<script> myname(); </script>
<script> weleW(Monique); </script>
<script> weleR(Monique); </script>
when I wrote the function in html it worked, but in the external file nothing happens.
Share Improve this question edited Oct 16, 2016 at 18:50 Mr Lister 46.6k15 gold badges113 silver badges155 bronze badges asked Oct 10, 2016 at 14:48 moniquemonique 1131 gold badge1 silver badge6 bronze badges 3- In external file? Where it is? Where is the error? – k102 Commented Oct 10, 2016 at 14:50
- You have a number of errors … all of which will be reported in your browser's developer tools' Console. They would also be reported if you had used a tool such as JS Hint. Read the error messages. – Quentin Commented Oct 10, 2016 at 14:50
- you should use quotes "" or '' for string in weleW( 'Monique' ) – Supersharp Commented Oct 10, 2016 at 14:51
8 Answers
Reset to default 0Monique , there is only one issue in your code, please use single quote (i.e 'Monique') or double quote ("Monique") when you are passing argument, i thing you are getting "Uncaught ReferenceError: Monique is not defined" error message.
<script> myname(); </script>
<script> weleW("Monique"); </script>
<script> weleR("Monique"); </script>
Or use this.
<script> myname(); </script>
<script> weleW('Monique'); </script>
<script> weleR('Monique'); </script>
If a <script>
has a src
then the text content of the element will be not be executed as JS (although it will appear in the DOM).
You need to use multiple script elements.
- a
<script>
to load the external script - a
<script>
to hold your inline code (with the call to the function in the external script)
You might have put the <script src="<your js file>" type="text/javascript"></script>
at the wrong place in your html file. Try putting it in the <head></head>
or before your body closing tag </body>
.
Another mon mistake is in <script src="<your js file>" type="text/javascript"></script>
try entering the location of like for example: <script src="./index.js" type="text/javascript"></script>
if your index.js file is in the same folder as your index.html.
Hope it helps!
From whay you described, you are calling your functions and linking the js file in Html correctly. There's just a couple things that are causing errors.
The weleR function has a littly syntax error. You are likely running into Uncaught SyntaxError: Unexpected identifier 'name' and *uncaught ReferenceError: myname is not defined". You needs to have "+" between the strings and name for the return statement of weleR to resolve this error. Use console.log() statements to check that your js file is running as you intended.
When you are calling the functions in your html file with , you should have an uncaught syntax error where Monique is not defined. You just need to change the weleW and weleR function from
<script> myname(); </script> <script> weleW(Monique); </script> <script> weleR(Monique); </script>
to
<script> myname(); </script>
<script> weleW("Monique"); </script>
<script> weleR("Monique"); </script>
so you are passing strings in.
With this, you should be able to get "MoniqueWele Monique!" for your output.
The last function weleR does not output anything into the output file because it is returning a str, and not changing the content of your external file. If you wish to have it also output into you file, write
weleR("Monique")
as
document.write(weleR("Monique"));
First of all, in your weleR
function, you missed the plus sign between each string or variable. After that, you need quotes around the word "Monique"
while running the code from the new script tags. Fixing this produces the desired result.
You forgot the + at the return. And you can just call the functions in your script.If you linked it correct then it should work.
And if you didn't know. Return doesn't print/write anything. It just returns. if you want to write it you just need to do this.
var weleR = return "Wele " + name + "!"; and document.write(weleR);
<script>
function myname(){
document.write("Monique");
}
function weleW(name){
document.write("<br/>" +"Wele " + name + "!" + "<br/>");
}
function weleR(name){
return "Wele " + name + "!";
}
myname();
weleW("Monique");
weleR("Monique");
</script>
1- correct some syntax error.
2- when you link to javascript file, documnet cannot see function in it untill it loaded, so script tags in your page exected before file has been loaded
you can fix your proplem by doing that
first add "defer" attribute to javascript link file
<script src="script.js" defer type="text/javascript"></script>
then you can write this code in your html page
document.onreadystatechange = () => {
if (document.readyState === "plete") {
myname();
weleW("Monique");
weleR("Monique");
}
Make sure you put the script function calls after the script src tag.