Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9123245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:17:32+00:00 2026-06-17T06:17:32+00:00

Possible Duplicate: Where to place Javascript in a HTML file? Should I write script

  • 0

Possible Duplicate:
Where to place Javascript in a HTML file?
Should I write script in the body or the head of the html?

I’ve always wondered, mainly because when creating pages I always run into trouble, based on the following thing:

When do you write your javascript

  • In the <head>
  • In the <body>
  • with a $(document).ready()

I could be stupid, but I’ve had a few times when my JavaScript (/jQuery) wasn’t executed because of the wrong place or yes or no doc.ready() command. So I’m really wondering so.

Same goes for jQuery and ‘var’ command

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:17:34+00:00Added an answer on June 17, 2026 at 6:17 am

    $(document).ready() simply ensures that all DOM elements are loaded before your javascript is loaded.

    When it doesn’t matter

    Without waiting for this event to fire, you may end up manipulating DOM elements or styles that are yet to exist on the page. the DOM ready event also allows you more flexibility to run scripts on different parts of the page. For example:

    <div id="map"></div>
    <script type="text/javascript">document.getElementById('map').style.opacity = 0;</script>
    

    will run because the map div has been loaded before the script runs. In fact, you can get some pretty good performance improvements by placing your scripts at the bottom of the page.

    When it does matter

    However, if you are loading your scripts in the <head> element, most of your DOM has not loaded. This example will not work:

    <script type="text/javascript">document.getElementById('map').style.opacity = 0;</script>
    <div id="map"></div>
    

    will not, since the map div has not been loaded.

    A safe solution

    You can avoid this by simply wait until the entire DOM has loaded:

    <script type="text/javascript">$(document).ready(function () { 
        document.getElementById('map').style.opacity = 0;
    });
    </script>
    <div id="map"></div>
    

    There are plenty of articles that explain this, as well as the jQuery documentation itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Where is the best place to put <script> tags in HTML markup?
Possible Duplicate: How bad is it to embed JavaScript into the body of HTML?
Possible Duplicate: Where is the best place to put <script> tags in HTML markup?
Possible Duplicate: Is it wrong to place the <script> tag after the </body> tag?
Possible Duplicate: Why split the <script> tag when writing it with document.write()? I don't
Possible Duplicate: Editing a text file in place through C# I need to replace
Possible Duplicate: using razor within javascript I would like to place a siimple value
Possible Duplicate: How do you round to 1 decimal place in Javascript? My Value
Possible Duplicate: Use JavaScript to place cursor at end of text in text input
Possible Duplicate: JavaScript/jQuery HTML Encoding I am passing info down to the client as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.