简体   繁体   中英

JavaScript: Prevent a button from being pressed before entire page is loaded

How can I prevent users from pressing my web-page buttons before the page is fully loaded?

ie I have a button that opens a lightbox. The JS for the lightbox is loaded last in the page for quicker response time. So if the user presses the button before the page is fully loaded - he either gets a JS error or the lightbox data is opened on a blank page.

I'm using Javascript with MooTools.

If I understand correctly neither

<body onload="...">

nor

$('#yourButton')...

guarantees that the document has finished loading. The (jQuery) method that does:

$(document).ready(function() { 
    $('#yourButton').attr("disabled", false); 
});

Make your button disabled. After the page is loaded - make it enabled

Here is a JQuery(sorry for not being "clean" Javascript example)

$('#yourButton').attr("disabled", false);

here's an example:

<body onload="document.getElementById('button1').disabled=false">
  <button id="button1" disabled="1">
    ok
  </button>
<body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM