Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript Function in JS File Won't Execute 1

Status
Not open for further replies.

nevadaflyfisher

Programmer
Mar 27, 2003
9
US
Greetings,

I'm new to JavaScript, but this problem seems ridiculously simple and I'm stumped. I've tried a few suggestions from other posts with no success.

I'm using IE6 on Win2K Pro and trying to execute a function in an external .js file. I double-checked to ensure that active scripting is enabled in the IE's security settings.

In an effort to oversimplify things, I created test.js with the following code:

Code:
function Howdy()
  {
    alert ("Howdy!")
  }

I call it from within the body of another HTML page:

Code:
<script language=&quot;javascript&quot; type=&quot;text/javascript&quot; src=&quot;test.js&quot;></script>

I also tried calling it from the document head without success.

I've tried a simple
Code:
document.write(&quot;Howdy&quot;)
statement in test.js and it works just fine.

Any idea as to what the heck might be wrong with calling the function?

TIA!
 
Can you show the code where you call the function? If the document.write works then you must have a typo when you're calling it...

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

 
Wow, thanks for the lightning fast response, Rick!

Here's everything that currently exists in the body:

Code:
<body>

<div id=&quot;navigation&quot;>
 <a href=&quot;Test3.html&quot;>CA/NV<br>Fly Fishing</a><br>
 <a href=&quot;Test3.html&quot;>Local Weather</a><br>
 <a href=&quot;Test3.html&quot;>Professional<br>Resume</a><br>
 <a href=&quot;Test3.html&quot;>Photos</a>
</div>

<div id=&quot;bg&quot;>
 <img src=&quot;images/bg.gif&quot; alt=&quot;&quot; width=&quot;100&quot;>
</div>
			
<div id=&quot;content&quot;>
 <h1>Welcome</h1>
 <script language=&quot;javascript&quot; type=&quot;text/javascript&quot; src=&quot;js/test.js&quot;></script>
</div>			

</body>
 
looks like you haven't called the function...

<body onLoad=&quot;Howdy()&quot;>

<div id=&quot;navigation&quot;>
<a href=&quot;Test3.html&quot;>CA/NV<br>Fly Fishing</a><br>
<a href=&quot;Test3.html&quot;>Local Weather</a><br>
<a href=&quot;Test3.html&quot;>Professional<br>Resume</a><br>
<a href=&quot;Test3.html&quot;>Photos</a>
</div>

<div id=&quot;bg&quot;>
<img src=&quot;images/bg.gif&quot; alt=&quot;&quot; width=&quot;100&quot;>
</div>

<div id=&quot;content&quot;>
<h1>Welcome</h1>
<script language=&quot;javascript&quot; type=&quot;text/javascript&quot; src=&quot;js/test.js&quot;></script>
</div>

</body>


Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

 
That was it!

I had perused a couple of other web sites for information on how to accomplish this and there was no mention whatsoever of the onLoad statement.

Thank you very much, Rick!
 
There are many methods to call functions. onLoad, onClick, onMouseOver, onChange, onBlur, onKeyPress and so on. No function ever fires on its own - it needs to be called (usually by an event handler like I've listed or by another function.

Example
<script>

alert(&quot;This alert will fire as soon as it is read by the browser&quot;)

function pageLoaded(){
alert(&quot;this alert will only fire when the function is called&quot;)
}


<body onLoad=&quot;pageLoaded()&quot;>

<input type=button value=&quot;Click Me&quot; onClick=&quot;alert('button was clicked')&quot;>
<br>
<a href=&quot;somePage.htm&quot; onMouseOver=&quot;pageLoaded()&quot;>A link</a>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top