0

I have a button on one html page, but when clicked I want it to activate a different button on a different html page. Is this possible?

3
  • Only in the some domain and when one of the pages is opened from the other. Commented Sep 21, 2017 at 18:22
  • Can a click event fire a click event in a separate browser tab? No. This is by design to prevent malicious behavior (hacking). Commented Sep 21, 2017 at 18:22
  • Can you put own JavaScript on the second page? Commented Sep 21, 2017 at 18:31

2 Answers 2

1

This is only possible if the first page actually generates the second page (via window.open()). Otherwise, you won't have any way to access the document in the other window.

Here's an example:

var newWin = window.open("other page in my domain");
var otherButton = newWin.document.querySelector("selector to find button");
otherButton.click();
Sign up to request clarification or add additional context in comments.

2 Comments

This is the case. The first button links to the second page, and I want clicking the first button to also click a button on the second page.
@Ebower No, linking to another page is not what I'm saying and will not work. The first page must programmatically cause the second window to be created with the second page inside of it.
0

This solution will work though, but it requires passing a value from one page to another. It cannot link pages together. If you want that look into websockets.

Page 1

HTML

<button id="activate">Activate other page</button>

JavaScript

document.querySelector("#activate").addEventListener("click", function(){
    location.href = "page2.html?button=activate"; //mind the querystring here.
});

Page 2

HTML

<button id="toactivate" disabled>disabled button</button> 

JavaScript

var activateButton = location.search.slice(1); //querystring | get rid of the ?
activateButton.split(/&/);
activateButton.forEach(function(value){
    var splitted = value.split("=");
    if (splitted[0] == "button" && splitted[1] == "activate")
    {
       document.querySelector("#toactivate").removeAttribute("disabled");
    } 
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.