0

I have this script that greys out the entire website and brings up a dialogue box when the text 'showPopUp' is clicked. My question is how can i get this script to execute automatically on page load?

Script:

<script type="text/javascript">
    function showPopUp(el) {
        var cvr = document.getElementById("cover")
        var dlg = document.getElementById(el)
        cvr.style.display = "block"
        dlg.style.display = "block"
        if(document.body.style.overflow = "hidden") {
            cvr.style.width = "1024"
            cvr.style.height = "100&#37;"
        }
    }

    function closePopUp(el) {
        var cvr = document.getElementById("cover")
        var dlg = document.getElementById(el)
        cvr.style.display = "none"
        dlg.style.display = "none"
        document.body.style.overflowY = "scroll"
    }
</script>
1

3 Answers 3

3
window.onload = function() {
  showPopUp('yourObjeID');
};

or

<body onload="showPopUp('yourObjeID');">

or insert script before body ends:

...
<script>
    showPopUp('yourObjeID');
</script>
</body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry about that, I did not notice. Edited.
1

Use window.onload. Documentation at https://developer.mozilla.org/en-US/docs/DOM/window.onload

Comments

0

The better aproach is to use the "document.ready" event, not window.load, because the load event is just triggered after ALL page resources are loaded, while the ready event is triggered after the document is ready (all HTML is loaded and the DOM is ready).

Note that the "document.ready" event is not an event implemented in JavaScript but "emulated" by some JavaScript libraries like jQuery.

Instead of writing an example, I got these two links, take a look:

3 Comments

Hey David, I don't think JavaScript has a "document.ready" event. Keep in mind this is tagged "JavaScript".
@jmort253: Yep, I know it (I used double quotes). I'm improving my answer ;)
Cool! Digging into the jQuery source and pulling out the JS! Awesome!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.