Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the earliest javascript/jquery-based method to execute javascript ASAP?

Is there a way to execute a few lines of javascript earlier than the document.ready event?

like image 344
2 revs Avatar asked Jan 06 '12 20:01

2 revs


People also ask

Where jQuery code is executed?

jQuery code runs on the web browser. In general, jQuery code is executed on the client side.

How is jQuery executed?

The jQuery starts its code execution from the $(document). ready() function which is executed whenever the whole HTML DOM is loaded and is totally rendered by the browser, so that the event handlers work correctly without any errors. This $(document).

Can you use jQuery in JavaScript?

jQuery is a JavaScript library, so it operates on top of JavaScript. It cannot exist on its own, so you can't use one over the other. You can use just JavaScript or JavaScript and jQuery. jQuery was introduced to make development with JavaScript easier.

Should I use jQuery or JavaScript?

To put things in a nutshell, jQuery is a better option while working with CMS websites like WordPress, Weebly, etc. It makes development easier because of the huge library of plugins it already has. However, even though jQuery is made from Javascript, ignoring vanilla Javascript is not a wise decision.


1 Answers

Yes, just don't put it in a handler. If you need access to elements, the only way as far as I know is to place the <script> element after the elements it needs to access in the document.

Here's a demo.

like image 59
Ry- Avatar answered Oct 07 '22 01:10

Ry-