Vanilla JS Solution

The HTML would remain the same, but you wouldn't need to load jQuery

<script type="text/javascript">
    window.onload = () => {
      const myModal = new bootstrap.Modal('#onload');
      myModal.show();
    }
</script>
Answer from ctmaloney on Stack Overflow
🌐
Bootstrap
getbootstrap.com › docs › 5.0 › components › modal
Modal · Bootstrap v5.0
Additionally, you may give a description of your modal dialog with aria-describedby on .modal. Note that you don’t need to add role="dialog" since we already add it via JavaScript. Embedding YouTube videos in modals requires additional JavaScript not in Bootstrap to automatically stop playback and more.
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-launch-bootstrap-modal-on-page-load.php
How to Launch Bootstrap Modal on Page Load
You can use the Bootstrap .modal('show') method for launching the modal window automatically when page load without clicking anything.
🌐
Bbbootstrap
bbbootstrap.com › home › snippets › bootstrap 5 how to launch bootstrap modal on page load
Bootstrap 5 How to launch Bootstrap modal on page load Example
This snippet is free and open source hence you can use it in your project.Bootstrap 5 How to launch Bootstrap modal on page load snippet example is best for all kind of projects.A great starter for your new awesome project with 1000+ Font Awesome Icons, 4000+ Material Design Icons and Material Design Colors at BBBootstrap.com.
Top answer
1 of 3
11

Vanilla JS Solution

The HTML would remain the same, but you wouldn't need to load jQuery

<script type="text/javascript">
    window.onload = () => {
      const myModal = new bootstrap.Modal('#onload');
      myModal.show();
    }
</script>
2 of 3
6

First of all welcome to Stackoverflow community.

As you have asked, you have correct the modal dialog code snippet first. You have missed the first line of modal in there. you have to add the id to that line, not to the modal-dialog div

<div class="modal fade" id="onload" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true"> <!-- Add this line to your code -->
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Do You Want Cookie? We Want Yours! </h5>
            </div>
            <div class="modal-body">
                This site uses cookies to personalies the content for you.
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div> <!-- And the relavant closing div tag -->

Then after that you need to import jquery to your code. Otherwise code jquery code segments will not work in your html file. Finally rather than using jquery onload event listner use the native VanillaJS window.onload event to trigger your modal.

<!-- import jquery -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<!-- Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

<!--Modal JS Script -->
<script type="text/javascript">
    window.onload = () => {
        $('#onload').modal('show');
    }
</script>

Then your bootstrap modal will work with expected behavior.

🌐
Bootstrap
getbootstrap.com › docs › 5.3 › components › modal
Modal · Bootstrap v5.3
They return to the caller as soon as the transition is started, but before it ends. In addition, a method call on a transitioning component will be ignored. Learn more in our JavaScript docs. Activates your content as a modal. Accepts an optional options object. const myModal = new bootstrap.Modal('#myModal', { keyboard: false })
🌐
MDBootstrap
mdbootstrap.com › standard › modal onload
Bootstrap Onload modal - free examples & tutorial
To make the modal appear immediately after loading the page, just remove the redundant button and add the corresponding JS function. Open the demo to see an example. ... <!-- Modal --> <div class="modal fade" id="myModal" tabindex="-1" ...
🌐
YouTube
youtube.com › watch
Show Modal on Page Load Bootstrap 5 - YouTube
👉 Professional JavaScript Course: https://bytegrad.com/courses/professional-javascript👉 Professional CSS Course: https://bytegrad.com/courses/professional-...
Published   November 23, 2022
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 77888884 › how-can-i-show-modal-on-page-load
bootstrap 5 - How can I show modal on page load? - Stack Overflow
<template id="my_template" name="Hello there"> <t t-call="portal.portal_layout"> <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="my_modal"> Show Modal </button> <!-- I want this modal to pop up on page load as weel as being able to toggle it with my button --> <div id="my_modal" tabindex="-1" role="dialog" class="modal fade"> <div class="modal-dialog modal-dialog-centered" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="my_modal_title">Hi</h5> <button type="button" class="btn text-danger" data-bs-dismiss="modal" aria-label="Close"> <span aria-hidden="true"><i class="fa fa-close"/></span> </button> </div> <div class="modal-body"> Content ...
🌐
CodePen
codepen.io › rajrs › pen › dMowNx
open bootstrap modal on page load
$(window).load(function(){ $('#myModal').modal('show'); }); ! 999px · Clear · Ctrl Ctrl Space Autocomplete · F Find · G Find Next · ⇧ G Find Previous · ⇧ Opt F Find & Replace · ⇧ F Format Code · [ Indent Code Right · ] Indent ...
Top answer
1 of 9
66

The modal plugin toggles your hidden content on demand, via data attributes or JavaScript. It also adds .modal-open to the to override default scrolling behavior and generates a .modal-backdrop to provide a click area for dismissing shown modals when clicking outside the modal. [source]

How to use via Vanilla JavaScript

Create a modal with a single line of JavaScript:

var myModal = new bootstrap.Modal(document.getElementById('myModal'), options)

source

A quick example:

var myModal = new bootstrap.Modal(document.getElementById("exampleModal"), {});
document.onreadystatechange = function () {
  myModal.show();
};
<!DOCTYPE html>
<html lang="en">
  <head>

    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />


    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/css/bootstrap.min.css"
      integrity="sha384-r4NyP46KrjDleawBgD5tp8Y7UzmLA05oM1iAEQ17CSuDqnUK2+k9luXQOfXJCJ4I"
      crossorigin="anonymous"
    />
    <title>Hello, world!</title>
  </head>
  <body>

    <div
      class="modal fade"
      id="exampleModal"
      tabindex="-1"
      role="dialog"
      aria-labelledby="exampleModalLabel"
      aria-hidden="true"
    >
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
            <button
              type="button"
              class="close"
              data-dismiss="modal"
              aria-label="Close"
            >
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            ...
          </div>
          <div class="modal-footer">
            <button
              type="button"
              class="btn btn-secondary"
              data-dismiss="modal"
            >
              Close
            </button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>

    <!-- JavaScript and dependencies -->
    <script
      src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
      integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/js/bootstrap.min.js"
      integrity="sha384-oesi62hOLfzrys4LxRF63OJCXdXDipiYWBnvTl9Y9/TRlw5xlKIEHpNyvvDShgf/"
      crossorigin="anonymous"
    ></script>

    <script src="./app.js"></script>
  </body>
</html>

As for your code, you didn't follow the right way to show/toggle a modal in Vanilla JavaScript. Why did you expect myModal.show() to happen while myModal is just a DOM element?

2 of 9
38
  1. Create a new instace of modal and then call show method(see usage points)
var myModal = new bootstrap.Modal(document.getElementById('exampleModal'))
myModal.show()

ref: https://v5.getbootstrap.com/docs/5.0/components/modal/#options

note: Bootstrap v5 no more uses jquery, instead it uses javascript. lots of changes are there in bootstrap v5, if you are working for a company and willing to use Bootstrap v5 then please go through all the changes.

🌐
Bootstrap
getbootstrap.com › docs › 5.2 › components › modal
Modal · Bootstrap v5.2
Another override is the option to pop up a modal that covers the user viewport, available via modifier classes that are placed on a .modal-dialog. Full screen Full screen below sm Full screen below md Full screen below lg Full screen below xl Full screen below xxl · <!-- Full screen modal --> <div class="modal-dialog modal-fullscreen-sm-down"> ... </div> ... As part of Bootstrap’s evolving CSS variables approach, modals now use local CSS variables on .modal and .modal-backdrop for enhanced real-time customization.
🌐
Tutorial Republic
tutorialrepublic.com › twitter-bootstrap-tutorial › bootstrap-modals.php
How to Create Modals with Bootstrap 5 - Tutorial Republic
You can easily create very smart and flexible dialog boxes with the Bootstrap modal plugin. The following example oulines the basic structure to create a simple modal with a header, message body and the footer containing action buttons for the user. ... <!-- jQuery Code (to Show Modal on Page Load) --> <script> $(document).ready(function() { $("#myModal").modal("show"); }); </script> <!-- Modal HTML --> <div id="myModal" class="modal fade" tabindex="-1"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">Confirmation</h5> <button type="but
🌐
Ordinarycoders
ordinarycoders.com › blog › article › bootstrap5-modal-cheatsheet
Bootstrap 5 Pop-up Modals: Cheat Sheet
Use the class attribute value model-fullscreen-lg-down to show a full-screen modal when the viewport is below 992px. Full-Screen Bootstrap 5 Modal on Viewpoints Extra Large and Below
🌐
SitePoint
sitepoint.com › javascript
How Toggle BootStrap 5 Modal Without Button Click
February 2, 2021 - I’m trying to learn Bootstrap 5 and I’m trying to toggle a modal on my page without using a button. By default a modal is hidden and normally the user needs to click on a button to show it. Instead of that I would like to automatically show the modal as soon as the page loads without the ...
🌐
ASPSnippets
aspsnippets.com › Articles › Open-show-Bootstrap-Modal-Popup-on-Page-Load.aspx
Open show Bootstrap Modal Popup on Page Load
April 2, 2022 - explained with an example, how to open Bootstrap Modal Popup on Page Load. The Bootstrap Modal Popup will be opened inside the JavaScript window.onload event handler which executes after the Page is loaded in Browser.
🌐
myprograming
myprograming.com › home › blog › how to add a bootstrap 5 modal popup form on page load – step-by-step guide
How to Add a Bootstrap 5 Modal Popup Form on Page Load – Step-by-Step Guide - myprograming
March 23, 2025 - <!--Modal JS Script --> <script type="text/javascript"> window.onload = () => { $('#myprograming').modal('show'); } </script> How to Add a Bootstrap 5 Modal Popup Form on Page Load – Step-by-Step Guide
🌐
Bootstrap
getbootstrap.com › docs › 4.0 › components › modal
Modal · Bootstrap
Be sure to add role="dialog" and aria-labelledby="...", referencing the modal title, to .modal, and role="document" to the .modal-dialog itself. Additionally, you may give a description of your modal dialog with aria-describedby on .modal. Embedding YouTube videos in modals requires additional JavaScript not in Bootstrap to automatically stop playback and more.
🌐
MDBootstrap
mdbootstrap.com › standard › material design for bootstrap 5 & vanilla javascript
modal on page load - Material Design for Bootstrap
Try using this to open the modal when your page is loaded: $('#myModal').modal('show'); Here are more options for modals https://mdbootstrap.com/javascript/modals/#advancedUsage
🌐
Studytonight
studytonight.com › bootstrap › how-to-launch-bootstrap-modal-on-page-load
How to launch Bootstrap modal on page load? - Studytonight
var myModal = new bootstrap.Modal(document.getElementById('myModal'), {}) myModal.show() Let's see an example to launch the modal automatically using the above code in Bootstrap 5.