如何在新窗口中获取dom元素? [英] How to get a dom element in a new window?

查看:481
本文介绍了如何在新窗口中获取dom元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript中的一个简单任务是打开一个新窗口并在里面写。但我需要写一个dom元素,一个带ID的div。

A simple task in JavaScript is to open a new window and writing inside. But I need to write in a dom element, a div with an ID.

var novoForm = window.open("somform.html", "wFormx", "width=800,height=600,location=no,menubar=no,status=no,titilebar=no,resizable=no,");

比我尝试的东西......

Than I try something...

var w = novoForm.innerWidth;
var h = novoForm.innerHeight;
novoForm.document.getElementById("monitor").innerHTML = 'Janela: '+w+' x '+h;

我这样做是为了查看对象novoForm是否有效。但是监视器div中没有​​写入任何内容。我也尝试使用onload事件但没有成功。我想知道这是否有一些安全限制,或者我错过了什么......

I did it to see if the object "novoForm" is valid. But nothing is written in "monitor" div. I also try using onload event with no success. I'm wondering if it's some security restriction or am I missing something...

推荐答案

或者,这是一个解决方案:

Alternatively, this is a solution:

var novoForm = window.open("somform.html", "wFormx", "width=800,height=600,location=no,menubar=no,status=no,titilebar=no,resizable=no,");
var teste = function(){
    var mon = novoForm.document.getElementById("monitor");
    if(typeof(mon)!="undefined"){
        //novoForm.alert("Achei!");
        var h = novoForm.innerHeight;
        var strh = String(h - 40 - 30)+'px';
        novoForm.document.getElementById("pagina").style.height = strh;
        novoForm.document.getElementById("monitor").innerHTML = '<p class="legenda">&copy; NetArts | gustavopi</p>';
        clearInterval(id);
    }
}
var id = setInterval(teste, 100);

这将完成这项工作。对我来说不是一个漂亮的解决方案,但它确实有效!

This will do the job. Not a "pretty" solution to me, but it works!

这篇关于如何在新窗口中获取dom元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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