tags:

views:

51

answers:

2
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.

+2  A: 

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.
Gabriel McAdams
I'd guess that this is part of a larger block of code - he said that it works in IE 7, Firefox and Opera. If there were a syntax error it wouldn't work at all.
Andy E
yeah i cleaned out some of that. the script executes, without error and i can alert out the window.location replacement. It seems for some reason chrome and safari can see the new window.location.href but they do not apply it.
local idiot
i tried the window.location.href = window.location.href; and it works in ie7, firefox, but not chrome or safari. i dont really care about safari so much.
local idiot
When it doesn't work, what happens?
Gabriel McAdams
instead of the href being changed to its current page, if you click an link, instead of the href getting changed to what you have in the variable, it follows through with the link or back button.
local idiot
Take a look at my modified answer. I think it will help.
Gabriel McAdams
A: 

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

Bryan Denny