最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Calling a javascript function from another .js file - Stack Overflow

programmeradmin0浏览0评论

I have two external .js files. The first contains a function. The second calls the function.

file1.js

$(document).ready(function() {

    function menuHoverStart(element, topshift, thumbchange) {

        ... function here ...

    } 

});

file2.js

$(document).ready(function() {

    setTimeout(function() { menuHoverStart("#myDiv", "63px", "myIMG"); },2000); 

});

The trouble is that this is not running the function. I need the two separate files because file2.js is inserted dynamically depending on certain conditions. This function works if I include the setTimeout... line at the end of file1.js

Any ideas?

I have two external .js files. The first contains a function. The second calls the function.

file1.js

$(document).ready(function() {

    function menuHoverStart(element, topshift, thumbchange) {

        ... function here ...

    } 

});

file2.js

$(document).ready(function() {

    setTimeout(function() { menuHoverStart("#myDiv", "63px", "myIMG"); },2000); 

});

The trouble is that this is not running the function. I need the two separate files because file2.js is inserted dynamically depending on certain conditions. This function works if I include the setTimeout... line at the end of file1.js

Any ideas?

Share Improve this question edited Nov 21, 2013 at 8:28 theLaw 1,2912 gold badges11 silver badges23 bronze badges asked Dec 2, 2010 at 12:41 TomTom 13k50 gold badges153 silver badges247 bronze badges 2
  • Is menuHoverStart declared in the global scope? – Andy E Commented Dec 2, 2010 at 12:45
  • possibly not - how do I go about doing that? – Tom Commented Dec 2, 2010 at 12:48
Add a comment  | 

2 Answers 2

Reset to default 56

The problem is, that menuHoverStart is not accessible outside of its scope (which is defined by the .ready() callback function in file #1). You need to make this function available in the global scope (or through any object that is available in the global scope):

function menuHoverStart(element, topshift, thumbchange) {
    // ...
}

$(document).ready(function() {
    // ...
});

If you want menuHoverStart to stay in the .ready() callback, you need to add the function to the global object manually (using a function expression):

$(document).ready(function() {
    window.menuHoverStart = function (element, topshift, thumbchange) {
        // ...
    };
    // ...
});

You have declared menuHoverStart inside a function (the anonymous one you pass to the ready ready). That limits its scope to that function and you cannot call it from outside that function.

It doesn't do anything there, so there is no need to hold off on defining it until the ready event fires, so you could just move it outside the anonymous function.

That said, globals are worth avoiding, so you might prefer to define a namespace (to reduce the risk of name collisions) and hang the function off that.

var MYNAMESPACE = {}; // In the global scope, not in a function
// The rest can go anywhere though
MYNAMESPACE.menuHoverStart = function (element, topshift, thumbchange) {
发布评论

评论列表(0)

  1. 暂无评论