1

Display dynamic value in confirm box. This is how i wanted it to work but it didnt. Can anyone tell me how it is done properly.

<script type='text/javascript'>
    setTimeout(function(){
        var url = document.URL;
        var r = confirm("Your session is about to be timedout in " + for (var i = 10; i > 0; i--){ i } + " seconds! Would you like to logged in?");
        if (r == true) {
            location = url;
        } else {
            location = '../logoff.php'
        }
    }, 10000)
</script>
2
  • Why is there a for-loop in your confirm-title? Even if this would work, you would get: Your session is about to be timedout in 10987654321 seconds! Would you like to logged in?... Is that what you want? Commented Nov 28, 2014 at 10:45
  • no i want a alertbox that changes dynamically Commented Nov 28, 2014 at 11:08

2 Answers 2

2

Aside from the flawed string concatenation logic, you cannot achieve this in a standard confirm box. The value is set when the box is instantiated and cannot be changed.

To do what you need you would need to use a modal plugin of some description which you have HTML/JS control over.

Sign up to request clarification or add additional context in comments.

Comments

0

This is a work around example:

$(function() {
  var out = $('#out'),
    sec;
  (function() {
    var th = setInterval(function() {
      sec = parseInt(out.text()) || 0;
      if (!sec) {
        clearInterval(th);
        timeout();
      } else {
        sec--;
        out.text(sec);
      }
    }, 1000);
  })();

  var timeout = function() {
    if ($('#in').prop('checked')) {
      alert('login ...');
    } else {
      alert('don\'t login ...');
    }
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<center>
  <h2>Your session is about to be timedout in <span id="out">10</span> seconds! Would you like to logged in?
  <br/>
  <input type='checkbox' id='in'/>Yes
    </h2>
</center>

5 Comments

this runs well but i would like my alertbox to have dynamic content and not in my span.
@Yesh, in that case, you will have to create a custom alert box, native alert/confirm box don't met your requirement!
any help on how to do that?
now i use a jquery and i made a file exclusively for the timeout and included it in all files on the site but the timer doesnt work in certain parts of the site. for example in my index.php it works but in my detail.php it doesnt work
Any errors on detail.php, have a look in the web browser console

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.