我正在尝试将 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/