开发者

Can javascript code, in a page returned by ajax, call a function that was in a js file of the original page using jquery

I have something similar to this:

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>JQuery Test</title>  
    <script type="text/javascript" src="js/jquery-1.5.1.min.js"></script>   
    <script type="text/javascript" src="js/page.js"></script>
</head>
<开发者_开发百科body>

<div id="searchResults" ></div>
</body>
</html>

The searchResults div will get populated by a jQuery .load(). The result that is returned into the div has javascript to be executed. This much works fine.

Now I need some of the javascript that gets returned into searchResults to call a function in page.js. This currently does not work. Is this possible?

UPDATE:

I sort of found the problem. In page.js the function I am trying to call is inside a $(document).ready statement. If declare the function outside of the document ready it works. Anyone care to explain to me why this is and is there a way to leave it in the document ready area?


Anyone care to explain to me why this is and is there a way to leave it in the document ready area?

It is not is the same scope as the other function. You know what I mean? It is hidden because it is inside another function (the document ready function). It is the same with a variable. If you declare a local variable inside a function, some code outside the function can't just directly reference that variable that is inside another function.


When you load into $('#searchResults') the document.ready() was already called so anything wrapped in those tags will not be done.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜