![](/img/trans.png)
[英]How to pass dynamic PHP variable as parameter in window.open method
[英]How to pass parameter in window.open method?
我在JavaScript中具有以下方法:
var string1 = "testing";
var date = "10/10/2012";
var win = window.open(BuildUrl(("report", "report"), "myreports",
"toolbar=0,statusbar=0,scrollbars=1,resizable=1,location=0");
function BuildUrl(controllerName, actionName) {
var TimeStamp = Number(new Date());
var win = window.location;
return win.protocol + "//" + win.host + "/" + controllerName + "/" + actionName + '?_=' + TimeStamp ;
}
C#控制器中的方法如下所示:
public ActionResult report()
{
return View();
}
现在,当访问C#方法时,我需要传递诸如名称和日期之类的参数。 我怎样才能做到这一点?
以以下格式将参数附加到URL:
<url>?param1=value1¶m2=value...
如果您需要传递值数组,请对参数使用相同的名称
<url>?arr=value1&arr=value2...
所以您的网址看起来像
domain.com/Controller/Report?name=xyz&date=20
更新:
要在Action中接收它们,请声明要传递给Action的参数。
public ActionResult Report(string name, DateTime date)
{
...
}
阅读ASP.NET MVC中的模型绑定
我找到了一种将参数传递到弹出窗口甚至从中检索参数的更好方法:
在主页中:
var popupwindow;
var sharedObject = {};
function openPopupWindow()
{
// Define the datas you want to pass
sharedObject.var1 =
sharedObject.var2 =
...
// Open the popup window
window.open(URL_OF_POPUP_WINDOW, NAME_OF_POPUP_WINDOW, POPUP_WINDOW_STYLE_PROPERTIES);
if (window.focus) { popupwindow.focus(); }
}
function closePopupWindow()
{
popupwindow.close();
// Retrieve the datas from the popup window
= sharedObject.var1;
= sharedObject.var2;
...
}
在弹出窗口中:
var sharedObject = window.opener.sharedObject;
// function you have to to call to close the popup window
function myclose()
{
//Define the parameters you want to pass to the main calling window
sharedObject.var1 =
sharedObject.var2 =
...
window.opener.closePopupWindow();
}
而已 !
这非常方便,因为:
玩得开心!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.