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>
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • possible duplicate of [how do I automatically execute javascript?](http://stackoverflow.com/questions/2644615/how-do-i-automatically-execute-javascript) – Basile Starynkevitch Nov 07 '12 at 06:01

3 Answers3

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

or

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

or insert script before body ends:

...
<script>
    showPopUp('yourObjeID');
</script>
</body>
</html>
Fred Wuerges
  • 1,965
  • 2
  • 21
  • 42
1

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

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
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:

Community
  • 1
  • 1
davidbuzatto
  • 9,207
  • 1
  • 43
  • 50