我试图将一个javascript变量传递给打开的新窗口,但是如果我添加了一个名为"_self“的变量,则不会传递该变量:
这是行不通的,appointmentId是未定义的:
    var w = window.open("../calendar/appointment.html","_self");
w.appointmentId = appt.id;  这样做是可行的:
    var w = window.open("../calendar/appointment.html");
    w.appointmentId = appt.id;如何使用_self名称传递变量,从而不会打开新的选项卡/窗口?
我传递的变量是巨大的。他们会接受URL限制。抱歉,我之前没有说明这一点。
谢谢
发布于 2014-03-27 17:12:37
另一种方法是在查询字符串中传递变量。
改变方向:
window.location = "../calendar/appointment.html?appt_id=" + appt.id在页面上加载:
<script type="text/javascript">
  // http://stackoverflow.com/a/901144/1253312
  function getParameterByName(name) {
      name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
      var regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
      var results = regex.exec(location.search);
      return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
  }
  window.appointmentId = getParameterByName('appt_id');
</script>发布于 2014-03-27 17:13:40
另一种方法是通过window.location实现这一点。例如:
window.location = "../calendar/appointment.html"; // this will send the user here in the current window此外,变量通常通过使用QueryString (有时称为GET变量)从页面传递到页面。这些通常出现在问号后面。在您的例子中,您可以像这样传递变量:
window.location = "../calendar/appointment.html?appointmentId=" + appt.id;
// ../calendar/appointment.html?appointmentId=113还可以在散列标记之后传递变量:
window.location = "../calendar/appointment.html#" + appt.id;
// ../calendar/appointment.html#113如果使用第二个选项,则可以通过location.hash读取变量。如果您通过QueryString传递它,那么您可以将变量从URL中提取出来,如下所示:
发布于 2014-03-27 17:19:07
window.open可以接受3个参数
https://developer.mozilla.org/en-US/docs/Web/API/Window.open
要传递的第二个参数是创建窗口的名称。这就是为什么你没有把它放在窗户里。
window.open(strUrl, strWindowName[, strWindowFeatures])有关支持的特性的信息可以在给定的url中找到。
https://stackoverflow.com/questions/22694420
复制相似问题