Yesh
Yesh

Reputation: 51

Display dynamic values in javascript alertbox

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>

Upvotes: 1

Views: 2242

Answers (2)

user2575725
user2575725

Reputation:

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>

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337620

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.

Upvotes: 2

Related Questions