开发者

Javascript function access

开发者 https://www.devze.com 2023-03-20 21:54 出处:网络
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.Att

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

精彩评论

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