local idiot
local idiot

Reputation: 1

Window.location Working But Not everywhere

var where_to = confirm("You have locked this record. You can only leave by clicking cancel.");

if (where_to== true) {
 alert(window.location);
 window.location.href = window.location;
 } else {
 window.location.href = window.location;
 }

This function works in IE 7 and firefox and opera 10.6. What it does is not allow a different page to load unless the user clicks a link or button that calls the token function.

The window.location.href is replaced with the current url unless the token function is called.

Thanks for your time.

Upvotes: 0

Views: 581

Answers (2)

Gabriel McAdams
Gabriel McAdams

Reputation: 58261

Your syntax is messed up. There are too many else statements.

This is the problem here:

} else {
  //alert(tokenset);
  alert('You have passed the token test');

}

EDIT

Based on your comments, it sounds like this is what you are trying to do: You want to be able to stop the user from leaving the page unless they do so through by clicking cancel.

In that case, you should be using the onbeforeunload event. Here is the IE page, and here is the Mozilla page on the event.

The idea is that any time the user tries to leave the page (even just by closing the browser or typing some other address in the address bar), this function is called.

The function is limited. You can only return a string. The browser uses this to prompt the user If they hit cancel, then they stay on the page. They can hit OK and continue leaving the page. Although you can only return a string, you don't have to. You can check for a condition, and only return a string if the condition passes. Look my this example:

window.onbeforeunload = function () {
    if (MyTokenIsNotSet) {
        return 'You have not released the lock';
    }
};

If your token is set, then the user will see nothing, and they will just leave the page. Otherwise, this will prompt the user to make sure they really want to leave the page.

A few of things to note:

  • The prompt looks different in the different browsers (the wording is different). I would try it out to make sure your string looks OK with the rest of the prompt.
  • This does not prevent the user from leaving. This only reminds the user that they SHOULD do something different. There is no way to reliably prevent the user form leaving a page.
  • In addition to this, you should handle the event the user's session ends (on the server) and release the lock at that time.

Upvotes: 2

Bryan Denny
Bryan Denny

Reputation: 27596

You have two else statements and an extra }

if (where_to== true)
{

  alert(window.location);

  window.location.href = window.location;

} else {

  alert('You have passed the token test');

}

Please fill us in with your code, you seem to be missing some of it?

Edit to your edit: Try removing the alert. Sometimes alerts can cause problems so it could be preventing your setting of window.location.href

Upvotes: 0

Related Questions