3 Replies - 7201 Views - Last Post: 18 January 2010 - 03:19 PM

#1 heady89  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

calling a function from a .js file?

Posted 18 January 2010 - 01:24 PM

Hi,

I am completly new to Javascript so bare with me as the noob i am.
Currently just trying to call functions from a .js file from my .html file

Here is the code, can't figure out why it dosn't work? It works if i put the medeltal(); into the .js file but i wanna call them from the html.
Note that if i put the medeltal(20, 10, 30, 40); into the JS file at top it works just fine, but i can't seem to get it to work from the html file :S

.js code

function medeltal(var1, var2, var3, var4)
{
	result = (var1 + var2 + var3 + var4) / 4;
	alert(result);
}




.xhtml ccode
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
   
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
	<head>
		<title>Uppgift 2</title>
		<meta http-equiv="content-type" content="text/html charset=iso-8859-1" />
	</head>

<body>

<script type="text/javascript" src="script.js">
medeltal(20, 10, 30, 40);
</script>

</body>
</html>


This post has been edited by heady89: 18 January 2010 - 01:27 PM


Is This A Good Question/Topic? 0
  • +

Replies To: calling a function from a .js file?

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,134
  • Joined: 07-September 06

Re: calling a function from a .js file?

Posted 18 January 2010 - 01:27 PM

Try changing this:
<script type="text/javacript" src="script.js">
medeltal(20, 10, 30, 40);
</script>


to this:
<script type="text/javacript" src="script.js"></script>
<script>
  medeltal(20, 10, 30, 40);
</script>


From that I have seen most browsers don't like it when you try to run javascript in the same tag you are loading a file from.

Hope that helps.
Was This Post Helpful? 1
  • +
  • -

#3 heady89  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 164
  • Joined: 31-December 09

Re: calling a function from a .js file?

Posted 18 January 2010 - 01:30 PM

Thank you , it seems to work.
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,134
  • Joined: 07-September 06

Re: calling a function from a .js file?

Posted 18 January 2010 - 03:19 PM

No problem, just glad to have been of service.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1