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

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

问题描述

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

  var google = window.open(http://google.com ) 

解决方案

阅读你的问题,似乎你想打开弹出的开发控制台?假设这是您要查找的内容,您应该能够右键单击弹出窗口并点击检查元素。然后从那里进入控制台。



如果您试图以编程方式从父项运行一个函数到弹出窗口中,这里有一个想法。



假设新窗口与您的域位于同一个域中,则此解决方案可能适用于您。 (浏览器支持有限)



在父页面上:

  //将函数存储在localStorage 
localStorage.runThis = function(){alert(Hello world); }
//打开弹出窗口
newWindow = window.open(http://your-domain.com/your-page);

在弹出的页面中打开:

  //检查函数是否已存储
if(typeof localStorage.runThis ===function){
//在localStorage中运行函数
localStorage.runThis();
}

一个问题是这种方法依赖于满足这个标准:




  • 浏览器支持localStorage

  • 父页面和弹出页面来自同一个来源
  • 弹出页面实际上必须查找有问题的函数并执行函数本身


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


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)

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆