0

Need to Display a popup before page load if page is loading more than 3 seconds. Used below code but it displays popup if page load is less tahn 3 seocnds also. Popup need to displayed if the page loading takes more time not less time.

<script type="text/javascript">
    setTimeout(fnShowPopup, 1000);
    function fnShowPopup() {
        var answer = confirm("It may take few time to open this docuemnt. Click YES if you want to open the docuemnt in native format or click on CANCEL to continue viewing the docuemnt")
        if (answer)
            window.open(NativeView())
  }
</script>
Kijewski
  • 25,517
  • 12
  • 101
  • 143

1 Answers1

1

setTimeout(func, delay) comes with a method to abort the timer: clearTimeout(timeoutID)

<script>
var myTimer = setTimeout(fnShowPopup, 3000);
if (typeof(window.addEventListener) === 'function') {
    // standard conforming browsers
    window.addEventListener('load', function () {
        clearTimeout(myTimer);
    }, true);
} else {
    // legacy, IE8 and less
    window.attachEvent('onload', function () {
        clearTimeout(myTimer);
    });
}
</script>

Put this in the <head> of your page, before any other <script>s, <style>s or <link>s.

In your fnShowPopup function you may want to stop the page loading if the user chooses the "native format". See https://stackoverflow.com/a/10415265/

Community
  • 1
  • 1
Kijewski
  • 25,517
  • 12
  • 101
  • 143
  • Getting "Microsoft JScript runtime error: Object doesn't support this property or method" at below lines: window.addEventListener('load', function () { clearTimeout(myTimer); }, true) – Sravya Potturi Apr 25 '13 at 11:16
  • Hm, I thought my prayers were heard and IE8 went extinct. Please see http://stackoverflow.com/questions/6927637/addeventlistener-in-internet-explorer – Kijewski Apr 25 '13 at 11:23
  • For the above code the popup is displaying after page loading . My requirement is that i have a IE with links if i click on a link it will open a new IE which is like a document viewer where we can annotate save print email the document so on. For example there can be a document with 3 pages or 100 pages or even 500 to 1000 pages. Large document will take more time to load a document. At that time a popup needs to be displayed. For the above example the popup is displayed after the page loads with some controls even for small documents and large documents. – Sravya Potturi Apr 25 '13 at 14:13
  • I need to display popup if the document or the viewer is taking more than 3 seconds not to the viewer which loads with in 3 seconds. – Sravya Potturi Apr 25 '13 at 14:15