I am using fancybox in an aspx page. The document ready function does not work in this page for a lightbox. Someone told me to write a new javascript code for loading the lightbox in that page.
Asked
Active
Viewed 7.4k times
30
-
3If you want to execute your script when everything is loaded use `$(window).load(function(){ // ...})` – Ram Sep 11 '12 at 09:15
-
@undefined, this is almost the same as `$(document).ready(function(){ ... })`. `load()` will wait until the graphics are also loaded. – alexbusu Sep 11 '12 at 09:19
-
Why is $(document).ready not working? You do have jQuery included right? – TheNameless Sep 11 '12 at 09:42
-
1try jQuery.noConflict() method first. – Buzz Sep 11 '12 at 09:44
5 Answers
47
- Include jQuery.
- Check network tab that you are not getting 404.
- Check console that you are not getting "$ is unknown".
Do stuff when DOM is ready.
$(function(){
// DOM Ready - do your stuff
});
-
6
-
3regardless of what the OP meant, I keep revisiting this page via Google (because I seem to forget this nifty convention). Thanks @user338195! – kingPuppy Dec 30 '14 at 19:43
42
Try this:
document.addEventListener('DOMContentLoaded', function() {
// ...
});
Works in modern browers and IE9+

Claudiu
- 3,700
- 1
- 38
- 35
-
Keep in mind that this won't work if the DOM has already loaded. If that's going to be a problem, take a look at these answers instead: https://stackoverflow.com/q/799981 https://stackoverflow.com/q/9899372 – Pikamander2 Sep 24 '19 at 12:26
7
You could use the standard js onload function to run if that's what your'e missing:
window.onload = function() {};
Do note that this might give you issues with libraries - I haven't investigated that.

LasseValentini
- 256
- 1
- 3
4
I believe using the script defer
tag is the best solution. For example,
<script src="demo_defer.js" defer></script>
More information at W3 Schools.

remjx
- 4,104
- 3
- 34
- 32
-
I actually didn't know about this and I kinda pride myself on knowing the common "unknowns" lol. Thanks! – E10 Sep 23 '21 at 16:55
3
best ways is to use like this:
jQuery.noConflict();
(function($) {
$(function() {
// by passing the $ you can code using the $ alias for jQuery
alert('Page: ' + $('title').html() + ' dom loaded!');
});
})(jQuery);

Lonare
- 3,581
- 1
- 41
- 45