views:

49

answers:

3

hi,

I'm loading from my main page an external js file, with a function.

I have a flash file in the main page invoking the javascript function.

Everything worked very well until when the javascript code was in the main file, but when I moved javascript to an external file the function seems not called anymore.

So... there is no way to move the javascript code to an external file ? Or any other solution ?

thanks

Update

Main file

...
<script type="text/JavaScript" src="../sites/all/themes/zen/zen/main.js" /></script>
</head>

Js file:

$(document).ready( function() {

    function changeSize(objectId, width, height) {

        alert("changeSize called");

...
A: 

As long as the files with the needed functions are included (and allowed to fully load) prior to the functions being called, then there is no difference between having the javascript placed in the document or in a separate file.

Are you certain that the external javascript is included correctly and that they are in the correct order?

Sean Kinsey
I've updated the question
Patrick
A: 

The two most likely causes of problems here are:

  • You have left HTML comments or XML CDATA markers in the JS when you moved it to an external file
  • You've got the URI wrong
David Dorward
(1) The JS file just contain the functions I wrote in the question, there are not other markers (or did I misunderstand you ?) (2) I've tested with FireBug and I don't get any "File Not found" error message. The file is loaded correctly
Patrick
+1  A: 

When you moved the JS to the external file, did you add $(document).ready( function() {?

Since you are wrapping the function in another function, you are limiting its scope, so it isn't global, which would make it very difficult to call it from outside the ready function (i.e. Flash wouldn't be able to find it).

David Dorward
ok cool thanks, now it works. So where should I use $(document).ready( function() ? I cannot use it in external files ? Or is it not longer necessary, since I'm loading an external file ?
Patrick
Use it when you want code to run when the DOM is ready. This is usually when the code inside it needs to access an element that appears after the script in the source order.
David Dorward