9

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 welcomeW(name){
    document.write("Welcome " + name + "!");
}

function welcomeR(name){
    return "Welcome " 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> welcomeW(Monique); </script>
<script> welcomeR(Monique); </script>

when I wrote the function in html it worked, but in the external file nothing happens.

Mr Lister
  • 44,061
  • 15
  • 107
  • 146
monique
  • 103
  • 1
  • 1
  • 4
  • In external file? Where it is? Where is the error? – k102 Oct 10 '16 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 Oct 10 '16 at 14:50
  • you should use quotes "" or '' for string in welcomeW( 'Monique' ) – Supersharp Oct 10 '16 at 14:51

4 Answers4

0

Monique , 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> welcomeW("Monique"); </script>
<script> welcomeR("Monique"); </script>

Or use this.

<script> myname(); </script>
<script> welcomeW('Monique'); </script>
<script> welcomeR('Monique'); </script>
Bhanu Pratap
  • 1,453
  • 14
  • 15
0

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)
-1

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 welcomeR = return "Welcome " + name + "!"; and document.write(welcomeR);

<script>
function myname(){
    document.write("Monique");  
}

function welcomeW(name){
    document.write("<br/>" +"Welcome " + name + "!" + "<br/>");
}

function welcomeR(name){
    return "Welcome " + name + "!";
}
myname();
welcomeW("Monique");
welcomeR("Monique");  
</script>
-2

Make sure you put the script function calls after the script src tag.

sam
  • 30
  • 2
  • There are plenty of problems with the code in the question, but nothing to suggest that the order of the script elements is one of them. – Quentin Oct 10 '16 at 14:58
  • "when i wrote the function in html it worked but in the external file nothing happens" – sam Oct 10 '16 at 15:02