Peculiar problem

Hi,
can someone explain why this happens

I have two files . trial.html and trial. js

trial.js

alert(“If this executes trial.js is loaded”);
function my_function()
{
(“hello world”);
}


trial.html

.
.
.
<script src=“trail.js”>
// alert() WORKS
my_function(); // DOES NOT WORK… the control does not go inside a fucntion

                                                 // the file is definitely loaded as the statment alert(); before the function defn executes

</script>

why does this happen. now if i tweak t a bit and put my_function() call in the js file

i.e

trial.js

alert(“If this executes trial.js is loaded”);
function my_function()
{
(“hello world”);
}
my_function(); //THIS WORKS ONCE REMOVED FROM the html file

why does this happen

Could you please post the actual source of the HTML and JS file as what you have above is quite confusing but i think i issue comes down to your function call not been wrapped in <script> tags. Also in your example code above you have what looks like an alert but the keyword is missing which would cause an error.

function my_function() {
    ("hello world");       // Won't work
    alert("hello world"); // Will work
}

You need to have 2 separate Script tags like this:

<script src="trail.js" type='text/javascript'> </script>
<script type='text/javascript'>

// alert() WORKS
my_function(); // DOES NOT WORK... the control does not go inside a fucntion 

// the file is definitely loaded as the statment alert(); before the function defn executes
</script>

When you think you are calling the function, the browser actually ignores that and just loads the stuff in your file…