使用Javascript中的弹出窗口将变量从一个html文档传输到另一个html文档
因此,我有一个名为使用Javascript中的弹出窗口将变量从一个html文档传输到另一个html文档,javascript,jquery,html,popup,Javascript,Jquery,Html,Popup,因此,我有一个名为commit\u PNG.html的主html文档,在这个文档中,我有两个简单的字符串变量,我想在另一个名为popup.html的html文档中使用。目前我有一个这样的函数: <script type="text/javascript"> function PopUpFenster(id) { myWindow = window.open('popup.html?id='+id, 'Info window', 'heigh
commit\u PNG.html
的主html文档,在这个文档中,我有两个简单的字符串变量,我想在另一个名为popup.html
的html文档中使用。目前我有一个这样的函数:
<script type="text/javascript">
function PopUpFenster(id) {
myWindow = window.open('popup.html?id='+id, 'Info window', 'height=350, width=800');
}
</script>
我不确定,但我需要直接从
commit\u PNG.html
获取它们,或者使用window.open()
方法解析它们。使用哈希部分传输JSON对象,如下所示:
<script type="text/javascript">
function PopUpFenster(id) {
myWindow = window.open('popup.html?id='+id, 'Info window', 'height=350, width=800');
}
</script>
在commit_PNG.html
中:
var string1 = "http://www.test.com/"+ commit_PNG.stringvariable1;
var string2 = "http://www.test.com/"+ commit_PNG.stringvariable2;
var myStrings = {
str1:"my first str",
str2:"my second str"
}
function PopUpFenster(id) {
var myUrl = "popup.html?id="+id+"#"+encodeURIComponent(JSON.stringify(myStrings));
window.open(myUrl , "Info window", "height=350, width=800");
}
然后在您的popup.html
中只需执行以下操作:
var myData = JSON.parse(decodeURIComponent(window.location.hash.substring(1)));
alert(myData.str1 + " " + myData.str2);
这是在url中传递日期的好方法。您可以传递一个JSON对象,并使用stringify和encodeURIComponent使其成为URL的安全字符串。
使用散列部分,确保未将其发送到服务器
提交PNG.html
函数getVariables(){
返回{
stringvariable1:'v1',
stringvariable2:'v2'
};
}
函数PopUpFenster(id){
myWindow=window.open('popup.html?id='+id,'Info window','height=350,width=800');
}
popup.html
var parentWindow=window.opener;
var variables=parentWindow.getVariables();
var string1=”http://www.test.com/“+variables.stringvariable1;
变量string2=”http://www.test.com/“+variables.stringvariable2;
那么您想将string1和string2传递到弹出窗口吗?不直接在弹出窗口中传递。我想用它们在第二个html文档中构建一个动态URL。稍后,通过这个URL,我会得到一些资源,这些资源将显示在弹出窗口中。效果非常好。我编辑了一点,就像丢失的=
。谢谢你的快速回复!
<script type="text/javascript">
var parentWindow = window.opener;
var variables = parentWindow.getVariables();
var string1 = "http://www.test.com/"+ variables.stringvariable1;
var string2 = "http://www.test.com/"+ variables.stringvariable2;
</script>