1

I have a web page with a countdown and redirect to the previous page which works great, but instead of redirecting to the previous page, I want the page (current browser tab) to close. disappear. self-destruct. I have tried substituting my redirect history.go(-1) with window.close() but it didn't work.

JS:

var countdownfrom=9    
var currentsecond=document.redirect.redirect2.value=countdownfrom+1

function countredirect(){
    if (currentsecond!=1){
    currentsecond-=1
    document.redirect.redirect2.value=currentsecond
    }
    else{
        history.go(-1)
    return
    }
    setTimeout("countredirect()",1000)
}    
countredirect()

HTML:

<form name="redirect">
    <h1>Oops! &nbsp;I think you meant to use a URL...</h1>
    <h2>this page will self destruct in</h2>
    <form>
        <input type="text" size="1" name="redirect2">
    </form>
    <h2>seconds</h2>
    <h3>Go back to where you came from and try again!</h3>

the html doesn't look right, can you even have a form nested in another form? It works fine though.

Corrected HTML:

    <h1>Oops! &nbsp;I think you meant to use a URL...</h1>
    <h2>this page will self destruct in</h2>
    <form name="redirect">
        <input type="text" size="1" name="redirect2">
    </form>
    <h2>seconds</h2>
    <h3>Go back to where you came from and try again!</h3>
MilkyTech
  • 1,919
  • 2
  • 15
  • 39
  • 2
    _"can you have a form nested in a form?"_ Nope. – j08691 May 15 '14 at 18:59
  • but for some reason it works. you can [see it live here](http://192.254.245.38) – MilkyTech May 15 '14 at 19:01
  • 5
    Browsers are very forgiving with bad code. Doesn't mean you should use it. – j08691 May 15 '14 at 19:02
  • last closing of form treated as first form's closing and the child form's closing tag added by the browser itself. so it is working... – Jai May 15 '14 at 19:03
  • ok, I've corrected the html, now I still need a solution for the js to close the current tab instead of going back a page – MilkyTech May 15 '14 at 19:11
  • I'm going with the workaround `open(location, '_self').close();`. It's working great in chrome even without user initiation. – MilkyTech May 15 '14 at 19:46

1 Answers1

5

This should be an informative answer: why doesn't my window.close work

Long story short, you probably are not meeting the criteria to close a window with JS.

Community
  • 1
  • 1
TheNorthWes
  • 2,661
  • 19
  • 35
  • 1
    Got to it faster than I could with a better definition. +1 – Brett Weber May 15 '14 at 19:18
  • ok, so `window.close` is prevented unless `window.open` was used to open the window. the workaround `open(location, '_self').close();` seems frowned upon and may not work for long. But the reason I want to close the window is because the only reason someone would be opening the window would be some sort of hacking attempt. – MilkyTech May 15 '14 at 19:27
  • What do you mean by hacking attempt? Someone could very easily turn JavaScript off and visit the page you are talking about and there is nothing you could do then. – TheNorthWes May 15 '14 at 19:30
  • i should clarify. the only way to get to the page is through an ip address which is the root for several virtually hosted sites – MilkyTech May 15 '14 at 19:30
  • I see. I don't know too much about this topic but I would hazard a *guess* that the right way to do this is server configuration to block serving that page. – TheNorthWes May 15 '14 at 19:32
  • before creating this page, the ip address would give you a directory listing of all the websites hosted – MilkyTech May 15 '14 at 19:32
  • I see. What server are you on? I would search SO or Google for 'Prevent web server directory listing' [Apache link](http://wiki.apache.org/httpd/DirectoryListings) – TheNorthWes May 15 '14 at 19:34
  • i called hostgator about the issue and they said they couldn't stop the directory listing on the ip and the best solution was for me to create an index.html page so they would get that instead of the directory. I just thought it would be cool for the page to self destruct – MilkyTech May 15 '14 at 19:35