javascript - Window.Open Javascript 变量

标签 javascript html

我正在尝试将 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 限制。抱歉,我没有提前指定这一点。

谢谢

最佳答案

另一种方法是在查询字符串中传递变量。

重定向:

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>

关于javascript - Window.Open Javascript 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22694420/

相关文章:

Javascript正则表达式从两个不同格式的文本 block 中获取特定字符串

javascript - 搜索打破可折叠 DIV

javascript - PHP 包含在 jQuery next() 冲突中

javascript - 使用 Jquery 搜索和隐藏文本

javascript - 我可以触发 soundcloud 小部件从播放器播放并在播放器外播放吗?

javascript - jquery 数据表编辑器 : "select" field displays option value instead of label

javascript检查交叉点降低计算复杂度

html - CSS定位图片和文字

html - 使用纯 HTML5/CSS3 创建标签云

javascript - 公开 View 状态的 AngularJS 指令