-2
<html>
    <head>
    </head>

    <body>
        <script>    
            var myFunction = function(){
                return "hello world";
            }
        </script>    
        myFunction();        
    </body>
</html>

I just finished the code academy course on javascript but it didn't taught me how to code javascript inside html. I need to display hello world inside the html's tag using the function is this possible? my friend told me I need jquery just to do this.

s3v3n
  • 8,203
  • 5
  • 42
  • 56

3 Answers3

2

You have to specifically tell the webpage to execute your function when it's loaded.

Instead of returning the string I believe you'd like to write it in the body of the webpage.

This is a simple way to do so :

<html>
    <head>
        <script>
            var myFunction = function(){
                document.body.innerHTML = "hello world";
            }
        </script>
    </head>
    <body onload='myFunction();'>
    </body>
</html>
Loic
  • 90
  • 6
-1
<html>
<head>
</head>
<body>
<Script>

function myFunction(){
    document.write("hello");
}


</script>
<Script>
    myFunction();
    </Script>

</body>
</html>

This was asked already How to call a javascript function within an HTML body

I just replace the

tag with document.write

Community
  • 1
  • 1
-2

Try this

<html>
<head>
</head>
<body onload='myFunction()'>

<script type="text/javascript">
    var myFunction = function(){
       return "hello world";
    }
</script>

</body>
</html>
Vishnu Prasad
  • 729
  • 1
  • 11
  • 28