what is the document ready equivalent without jQuery

0 votes
I have a script that uses $(document).ready, but it doesn't use anything else from jQuery. I'd like to lighten it up by removing the jQuery dependency.

How can I implement my own $(document).ready functionality without using jQuery? I know that using window.onload will not be the same, as window.onload fires after all images, frames, etc. have been loaded.
Apr 2, 2020 in Java-Script by kartik
• 37,510 points
20,677 views

1 answer to this question.

0 votes

Hello @kartik,

There are three options:

  1. If script is the last tag of the body, the DOM would be ready before script tag executes
  2. When the DOM is ready, "readyState" will change to "complete"
  3. Put everything under 'DOMContentLoaded' event listener

onreadystatechange

  document.onreadystatechange = function () {
     if (document.readyState == "complete") {
     // document is ready. Do your stuff here
   }
 }

DOMContentLoaded

document.addEventListener('DOMContentLoaded', function() {
   console.log('document is ready. I can sleep now');
});

Thank you!!

answered Apr 2, 2020 by Niroj
• 82,880 points

Related Questions In Java-Script

0 votes
1 answer

What is the difference between node.js and io.js?

Hello, io.js: Node-forward is basically being merged into io.js forked on ...READ MORE

answered Apr 24, 2020 in Java-Script by Niroj
• 82,880 points
628 views
0 votes
1 answer

How to get the jQuery $.ajax error response text?

Hello @kartik, Try: error: function(xhr, status, error) { ...READ MORE

answered Jul 27, 2020 in Java-Script by Niroj
• 82,880 points
29,265 views
0 votes
1 answer

What is the “hasClass” function with plain JavaScript?

Hello @Kartik, Simply use classList.contains(): if (document.body.classList.contains('thatClass')) { ...READ MORE

answered Sep 2, 2020 in Java-Script by Niroj
• 82,880 points
835 views
0 votes
0 answers

Jquery validation plugin - TypeError: $(...).validate is not a function

I am getting an error in my ...READ MORE

May 7, 2022 in Java-Script by narikkadan
• 63,420 points
1,120 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
21,880 views
0 votes
1 answer

What is type casting and type juggling in php?

Hey, The way by which PHP can assign ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,880 points
6,833 views
0 votes
2 answers

How can we create a session in PHP?

Hello, niroj. Here is my idea session_start(); $_SESSION['USERNAME'] ...READ MORE

answered Dec 7, 2020 in PHP by Famous
• 140 points
931 views
0 votes
1 answer

What is the use of $_REQUEST variable in php?

Hii @kartik, The $_REQUEST variable is used to read the ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,880 points
2,810 views
0 votes
1 answer

Error:$(document).ready equivalent without jQuery

Hello @kartik, Extends DOMContentLoaded so that it can ...READ MORE

answered Jun 8, 2020 in Java-Script by Niroj
• 82,880 points
737 views
0 votes
1 answer

How can I implement my own $(document).ready functionality without using jQuery?

Hello @kartik,  Use this: var docLoaded = setInterval(function () ...READ MORE

answered Aug 28, 2020 in Java-Script by Niroj
• 82,880 points
456 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP