3

My work is building some websites for battered women. They have requested an "Escape" button to quickly leave the page in case they get walked in on.

The examples they gave me are either a normal link (where you can click "back" and be on the previous page) or a convoluted solution with popups and new windows still resulting in a simple "back" to see where they were. Personally I don't think either of those is a great idea. I have searched and tried several ways of closing the current window and opening another but I haven't been able to find a solution that works across the major browsers. If anyone knows of how to do this or has a better idea on how to solve their issue I'd appreciate it.

This post (Html javascript to open new window and close current window) works in chrome but unfortunately I need it to work in at least IE, Firefox and Chrome.

Community
  • 1
  • 1
Jere.me
  • 291
  • 3
  • 14

6 Answers6

0

You may take a look at Mousetrap. It makes doing keybindings relatively simple. Using it, you could do something like this:

Mousetrap.bind('esc', function(e) {
    window.location.history.back();
    // Or alternatively, window.close() to just close the window
    return false;
});
Julio
  • 2,261
  • 4
  • 30
  • 56
0

Maybe try this:

< a href="javascript:window.opener='x';window.close();">Close< /a>

Armando Ortiz
  • 81
  • 1
  • 1
0

Wouldn't using window.location.replace do the trick?

http://jsfiddle.net/atesgoral/xS3q5/

Clicking the browser's Back button won't bring the previous page because the history will be overwritten.

Ates Goral
  • 137,716
  • 26
  • 137
  • 190
  • Thanks but this seemed to work in Chrome but not FF or IE. Seemed like a good suggestion though. There are some scripts that work in each of the browsers so I suppose I could have three different buttons and test what browser the person is using and display the one works in their particular browser? This seems like a bad hacky way of doing what I want though. – Jere.me Feb 14 '13 at 02:21
0

As far as I know, there is no fully cross-browser compatible way to close the current window. What I would suggest doing instead is just having Esc navigate to some other website or page. You can make a page on your website that displays a random article from Google News or something, or you can just go there directly. Unless your website is called battered-women.com or something, it would probably achieve the same effect as closing the window entirely.

document.addEventListener('keydown', function (e) {
    var key = e.keyCode ? e.keyCode : e.which;
    if (key == '27') {
        //destroy all visible content in case new page takes a long time to load
        document.getElementsByTagName('body').innerHTML = '';
        window.location = 'http://news.google.com/';
    }
});
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

What about replacing the whole page using JavaScript with a blank div? The problem with solutions that require changing the page is that you cannot control network latency. Removing all of the content from the page would be somthine you have complete control over.

The solution below merely shows a div with a 100% height and width with a fixed position, hiding any content on the page. When the page is hidden you could safely change the location without worrying about load times.

<div id="HideMe" style="display: none; position: fixed; width: 100%; height: 100%; background-color: #fff">&nbsp;</div>
<a href="#" id="escape">Escape!</a>

and

$("#escape").on("click",function(){
    $("#HideMe").show();
    location.href = "/*New URL*/";
});

http://jsfiddle.net/ySq8L/

marteljn
  • 6,446
  • 3
  • 30
  • 43
0

Between several of us (and the internet) we couldn't find a great solution for what they were looking for. After talking more with the client we've agreed to make a standard link to a neutral page but include a disclaimer about their online safety with a link on how to do private browsing. It's not nearly as elegant as any of us were hoping but it's our best solution so far.

Thanks for the suggestions, some had some good promise but didn't fully get what the client wanted.

Jere.me
  • 291
  • 3
  • 14