如何从主选项卡在新选项卡上运行功能?(谷歌浏览

How to run function on new tab from main tab? (Google Chrome)(如何从主选项卡在新选项卡上运行功能?(谷歌浏览器))

本文介绍了如何从主选项卡在新选项卡上运行功能?(谷歌浏览器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要打开控制台并在新选项卡上运行一个功能,该功能是我使用 javascrip 打开的.打开部分很简单,但是如何在其他选项卡上运行功能?

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")

推荐答案

在阅读您的问题后,您似乎正在寻找打开开发控制台的弹出窗口?假设这是您要查找的内容,您应该能够右键单击弹出窗口并点击检查元素".然后从那里转到控制台.<小时>如果您尝试以编程方式从父级运行一个函数到弹出窗口,这里有一个适合您的想法.

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)

在父页面上:

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

在弹出的页面上打开:

//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:

  • 浏览器支持本地存储
  • 父页面和弹出页面来自同一来源
  • 弹出页面必须实际查找相关函数并执行函数本身

这样做的一个缺点是,如果有人去 Javascript 控制台并将他们自己的函数设置为 localStorage,弹出页面会看到他们的函数并运行潜在的危险代码 - 一个安全漏洞.

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.

这篇关于如何从主选项卡在新选项卡上运行功能?(谷歌浏览器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何从主选项卡在新选项卡上运行功能?(谷歌浏览

基础教程推荐