JavaScript变量和框架的范围

时间:2012-06-12 07:20:25

标签: javascript scope frames

我有不同的javascript文件和带框架的网站。一般来说,Javascript.js我写了这个

console.log("BP0 - generalJavaScript.js eingebungen;");
top.window.clickedNewForm = false; 
function setClickedNewForm(booleanobj){
    console.log("BP5 - setClickedNewForm(); top.window.clickedNewForm: " + top.window.clickedNewForm + ";");
    top.window.clickedNewForm = booleanobj;
    console.log("BP6 - top.window.clickedNewForm: " + top.window.clickedNewForm + ";");
}
function getClickedNewForm(){
    console.log("BP7 - getClickedNewForm();");
    return top.window.clickedNewForm;
}

在Firefox-Console中我可以看到这样的消息:

Permission denied to access property 'clickedNewForm'
generalJavaScript.js()genera...ript.js (Zeile 11)
[Bei diesem Fehler anhalten]    
top.window.clickedNewForm = false;

有什么问题?我想如果我在顶层对象上声明了一个全局变量,它可以从所有帧中访问吗?!

UPDATE:尝试使用parent.myvar而不是top.window,遗憾的是没有任何改变,FF-Console:

BP0 - generalJavaScript.js eingebungen;
genera...ript.js (Zeile 10)
Permission denied to access property 'clickedNewForm'
generalJavaScript.js()genera...ript.js (Zeile 12)
[Bei diesem Fehler anhalten]    
parent.clickedNewForm = false;

1 个答案:

答案 0 :(得分:1)

您需要使用父对象来访问父框架中的值。

console.log("BP0 - generalJavaScript.js eingebungen;");
parent.clickedNewForm = false; 
function setClickedNewForm(booleanobj){
    console.log("BP5 - setClickedNewForm(); parent.clickedNewForm: " + parent.clickedNewForm + ";");
    parent.clickedNewForm = booleanobj;
    console.log("BP6 - parent.clickedNewForm: " + parent.clickedNewForm + ";");
}
function getClickedNewForm(){
    console.log("BP7 - getClickedNewForm();");
    return parent.clickedNewForm;
}