开发者

Javascript function access

I have a block of code in $(function() { function parseData(){...} )};

I also have a function declared outside of the ready block. This is outside since I hook it up in codebehind ie ddlCategories.Attributes.Add("onchange", "getDataFields()");

From the getDataFields function, I need to call parseData but it does not seem to find it.

How do I access my parseData() which is in my ready block, from outside of the ready block?

(sorry if some of terminology is off - I am now to JS开发者_开发问答)


Any reason parseData() has to be in the ready block? Why not just make it a top level function:

<script type="text/javascript">

function parseData() { ... }

$(document).ready( function() {
   ddl.Categories.....
}
<script>


Just define it outside your ready block. It's currently inaccessible due to the scope of the function.

Definitions can always safely go outside $(function() { ... }) blocks because nothing is executed.

$(function() { ... });

function parseData() { ... }

However, you seem to have a syntax error: )}; should be });.


Declare your parseData function outside of the self-executing $(function(){}); block.

var parseData = function () { ... };

$(function() { ... });

If there are things you need to reference inside the self-executing function, then declare the function globally but define it inside the self-executing function:

var parseData;

$(function() { parseData = function () { ... }; });

As an aside, declaring functions or variables globally is a bad idea.


You should declare the function outside the $(function() { ... }) callback.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜