-1

I am trying to add a button to my HMTL form that will allow me to close the tab that is being opened in the web browser for google recaptcha v3. Not only would I like to close the tab that was opened by the program, but I would also like to terminate the program after clicking the button. Any ideas on what I am doing wrong with the following code.

I understand that I have declared 'let myWindow;', but I am not assigning anything to it. I am new to HTML, so I was wondering if I could receive some advice on how to accomplish this. Thank you.

**<button onclick="closeWin()">Close Window</button>**    

**let myWindow;

function closeWin() {
    myWindow.close();
}**

Entire Source Code:

<?php
    define('SITE_KEY', '6Ldl-98fAAAAAKRPodblUlTcVgvrfWZ_8lODjmZA');
    define('SECRET_KEY', '6Ldl-98fAAAAAD3ekajHHVBi2X4fZTW37bI5IGUN');
    
    if($_POST){
        function getCaptcha($SecretKey){
            $Response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".SECRET_KEY."&response={$SecretKey}");
            $Return = json_decode($Response);
            return $Return;
        }
        $Return = getCaptcha($_POST['g-recaptcha-response']);
        //var_dump($Return);
    
        echo '<script type="text/javascript">alert(
             "\$Return->success: ' . $Return->success .
        "\\n\$Return->score: " . $Return->score . '");</script>';
    
        if($Return->success == true && $Return->score > 0.3){
            echo "Success!";
        }else{
            echo "Bot detected!";
        }
    }
    
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>ReCaptcha V3</title>
    <script src='https://www.google.com/recaptcha/api.js?render=<?php echo SITE_KEY; ?>'></script>
</head>
<body>
    <form action="/" method="POST">
        <input type="text" name="name" /><br />
        <input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response" /><br />
        <input type="submit" value="Submit" />
        **<button onclick="closeWin()">Close Window</button>**
    </form>
    <script>
    grecaptcha.ready(function() {
    grecaptcha.execute('<?php echo SITE_KEY; ?>', {action: 'homepage'})
    .then(function(token) {
        //console.log(token);
        document.getElementById('g-recaptcha-response').value=token;
    });
    });

    **let myWindow;

    function closeWin() {
        myWindow.close();
    }**
    </script>
</body>
</html>

1 Answer 1

0

If the tab has been opened with javascript, you have to assign the name of the tab to the variable. Tabs that have not been opened with javascript cannot be closed with code. Reference: Scripts may not close windows that were not opened by script.

Not the answer you're looking for? Browse other questions tagged or ask your own question.