문제

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>
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top