user1899082
user1899082

Reputation:

How do I call individual methods from a Javascript file

In the javascript assets file I have a jstester.js file like this:

function hehe()
{
    alert("wedsdsd");
}
document.write("fdygsdfysdgf");

Then in the public index.html file I have this:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<script src="/assets/jstester.js">
hehe();
</script>

</body>
</html>

So I thought that is how I can call a method from my Javascript file but looks like it is not working, no message box shows up... So what is the correct way of doing this?

Upvotes: 2

Views: 37

Answers (2)

ted
ted

Reputation: 5329

If a <script> has a src then the text content of the element will be ignored. so you can't do:

<script type="text/javascript" src="assets/jstester.js">
  hehe();
</script>

but:

<script type="text/javascript" src="assets/jstester.js"></script>
<script>hehe();</script>

Upvotes: 1

xzhang
xzhang

Reputation: 562

This is what you looking for:

<body>
    <script src="/assets/jstester.js"></script>
    <script type="text/javascript">hehe();</script>
</body>

Upvotes: 1

Related Questions