Home > Web Front-end > JS Tutorial > body text

How to Display a Bootstrap Modal Automatically on Page Load Without JavaScript?

Patricia Arquette
Release: 2024-10-19 15:57:30
Original
830 people have browsed it

How to Display a Bootstrap Modal Automatically on Page Load Without JavaScript?

Launching Bootstrap Modal on Page Load Without JavaScript Knowledge

The Bootstrap documentation advises using JavaScript to launch a modal, but it can be daunting for those unfamiliar with the language. However, launching a modal on page load is possible even without JavaScript proficiency.

Solution:

To display a modal immediately upon page load, embed it within a jQuery load event in the document's head section:

<code class="html"><script type="text/javascript">
    $(window).on('load', function() {
        $('#myModal').modal('show');
    });
</script></code>
Copy after login

HTML Code:

<code class="html"><div class="modal hide fade" id="myModal">
    <div class="modal-header">
        <a class="close" data-dismiss="modal">×</a>
        <h3>Modal header</h3>
    </div>
    <div class="modal-body">
        <p>One fine body…</p>
    </div>
    <div class="modal-footer">
        <a href="#" class="btn">Close</a>
        <a href="#" class="btn btn-primary">Save changes</a>
    </div>
</div></code>
Copy after login

This code ensures that the modal with the ID "myModal" appears as soon as the page loads.

Additional Notes:

  • You can still launch the modal manually by adding a link like this:
<code class="html"><a class="btn" data-toggle="modal" href="#myModal">Launch Modal</a></code>
Copy after login
  • This method works without any JavaScript dependencies and is ideal for those who prefer to avoid coding or have limited knowledge of it.

The above is the detailed content of How to Display a Bootstrap Modal Automatically on Page Load Without JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!