开发者

Javascript: can I call a function in an external file from the main page?

开发者 https://www.devze.com 2022-12-30 15:41 出处:网络
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.

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");

...


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).


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


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号