2

I need to open console and run one function on new tab, that I opened using javascrip. The opening part is easy, but how to run function on other tab?

var google = window.open("http://google.com")
Matt V.
  • 43
  • 4
  • I don't believe this would be possible... it would be a security breach if sites could access and run functions on other sites... Consider the following: Website asks you to login to Google, opening a popup to login. The webpage runs a function on the Google page to take your password from the input fields and send it to a server. I imagine this senario must have gone through their heads when designing the popup system. The only way I can think of is, and only if the popup is on the same domain, to use localStorage to store a function, and have the popup page run the function itself. – MineAndCraft12 Jul 05 '16 at 22:27

2 Answers2

2

Upon reading your question, it seems you're looking to open the dev console for the popup? Assuming this is what you're looking for, you should just be able to right-click the popped-up window and hit 'Inspect Element'. Then go to the console from there.


If you're trying to programatically run a function from the parent onto the popup window, here's an idea for you.

Assuming the new window is on the same domain as yours, this solution may work for you. (browser support is limited)

On the parent page:

//store the function in localStorage
localStorage.runThis = function(){ alert("Hello world"); }
//open the popup window
newWindow = window.open("http://your-domain.com/your-page");

On the page to open in the popup:

//check if the function has been stored
if(typeof localStorage.runThis === "function"){
    //run the function in localStorage
    localStorage.runThis();
}

One issue is that this method relies on this criteria being met:

  • Browser supports localStorage
  • Parent page and Popup page come from the same origin
  • Popup page must actually look for the function in question and execute the function itself

One drawback of this is that if someone were to go to the Javascript Console and set their own function into localStorage, the popup page would see their function and run potentially dangerous code - a security hole.

MineAndCraft12
  • 671
  • 5
  • 15
  • Thanks for your answer, but I need to open new tab, press button on child tab and then after few second close child tab. I don't have problems with opening and closing child tab, but I don't know how to run function or command on child tab from parent tab, because script is running at parent tab and it should repeatedly open same page, push one button and close it. – Matt V. Jul 09 '16 at 16:47
  • Perhaps when the button is pushed on the child page, you could save the choice in localStorage before closing the child page from that button, then when the parent sees that the child has been closed by pushing the button, it checks localStorage for the choice? For instance, on child page, have button do: `localStorage.choice = (whateverTheChoiceIs); window.close();`, and on the parent page have the script for the child.onclose check the localStorage.choice value? – MineAndCraft12 Jul 10 '16 at 17:46
0

A common solution is using localstorage.

if (typeof(Storage) !== "undefined") {
  // Code for localStorage/sessionStorage.
  localStorage.setItem("lastname", "Smith");
  var lastname = localStorage.getItem("lastname");
} else {
    // Sorry! No Web Storage support..
}
Erik Engervall
  • 269
  • 1
  • 8