0

I am using jquery with asp .net. I have some javascript code in some custom controls that I want to execute when the documents loads.

To execute the code I use this snippet:

$(document).ready(function () {
    /*do work here*/ 
});

My question is that if I set that handler in the control, won't it override the handler for document.ready in the page that contains the control?

Alecu
  • 2,627
  • 3
  • 28
  • 51

2 Answers2

1

You can use anonymous function in your user control script, that way you are safe always

var results= function () {
  //Do your work here      
}

Although it's perfectly legal to use multiple document.ready functions in your page, please check below

Can you have multiple $(document).ready(function(){ ... }); sections?

Community
  • 1
  • 1
Pratik
  • 1,472
  • 7
  • 20
  • 36
  • @Satpal Thanks for pointing that, it was new to me also (my bad). Corrected to want i want to answer. – Pratik Jun 13 '13 at 11:57
1

No, it's not overriding just adding. Such code will produce two alerts for example:

<script type="text/javascript">
$(document).ready(function () {
    alert("hello");
});

$(document).ready(function () {
    alert("world");
});
</script>

Live test case.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208