简体   繁体   中英

Click a button in a tab with javascript

just wondering and not sure if this can even be done but is there anyway to click a button in a new tab that opens on button click with javascript?

<a href="#" id="target">Open Link</a>


(function() {
    var button = document.getElementsByClassName('btn btn-green')[0];
    document.getElementById("target").onclick = function() {
        var wnd = window.open("//LinkHere.com/");
        setTimeout(function() {
            wnd.button.click();
            setTimeout(function() {
                wnd.close();
            }, 2000);
        }, 5000);
        return false;
    };
})();

I dont know if what you are asking is possible without parsing URL parameters to the next page. Assuming you are the owner (developer) of the page you are redirecting to, the below is my proposed solution:

<!-- Initial page -->
<html>
<head>
    <script type="text/javascript">
        document.getElementById('target').onclick = function() {
            var wnd = window.open("myLink?clickButton=true");
        }
    </script>
</head>
<body>
    <a href="#" id="target">Open link</a>
</body>

<!-- Other page -->
<html>
<head>
    <script type="text/javascript">
        var urlString = window.location;
        var url = new URL(urlString);
        var clickButton = url.searchParams.get("clickButton");

        if(clickButton === 'true'){
            document.getElementById("myButton").click();
        }
    </script>
</head>
<body>
    <button id="myButton">Click me</button>
</body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM