javascript 如何访问子窗口内的父窗口javascript变量(弹出窗口)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14941504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to access parent window javascript variable inside child window(pop-up)?
提问by emilly
I have html page which has global javascript variable with name custName="scott"
. I open the pop-up window with window.open
.
我有一个 html 页面,其中包含 name 的全局 javascript 变量custName="scott"
。我用window.open
.打开弹出窗口。
Now if I access the custName
inside pop-up window with window.opener.custName
, I get values as undefined
. How do I access parent window javascript variable inside child window(pop-up)?
现在,如果我使用 访问custName
内部弹出窗口window.opener.custName
,我会得到值为undefined
。如何在子窗口(弹出窗口)中访问父窗口 javascript 变量?
回答by emilly
window.opener.custName
works. It was typo mistake.
window.opener.custName
作品。这是打字错误。
回答by Naveen D Almeida
Try this to get the Query string in javaScript
试试这个来获取 javaScript 中的查询字符串
link.html?page=1
<script type="text/javascript">
$.getUrlVar('page');
$.extend({
getUrlVars: function(){
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
},
getUrlVar: function(name){
return $.getUrlVars()[name];
}
});
</script>