繁体   English   中英

想要将JavaScript变量从一个php模板传递到另一个

[英]Want to pass JavaScript variable from one php template to another

将一个JS变量传递给另一个JS时遇到一些问题。 我正在使用HubSpot表单及其事件onFormSubmit

redirectUrl: "/lp-thank-you-page/",
onFormSubmit: function($form) {
var email = document.getElementById("email-feca4800-6b73-4a38-8f80-1406a1b726b7").value;
}

基本上我想使用可变电子邮件到另一个页面模板,这是谢谢页面。 在“谢谢”页面上,我必须将用户重定向到他们的特定电子邮件,因此我的url参数应如下所示: login?req = md-freetier-signup&src = 电子邮件,并且我正在使用此电子邮件

<script>
setTimeout('Redirect()', 5000);
function Redirect()
{
    window.location="https://myurl.com/login?req=md-freetier-signup&src="+email;
}
</script>

但是我很确定该值根本不会传递给该其他模板,并且我不确定如何使它传递,这样我的代码才能工作。

提前致谢!

您可以将该变量保存在会话中,以便以后访问。 这将绕过HubSpot,并允许您随时检索这些变量。

例:

redirectUrl: "/lp-thank-you-page/",
onFormSubmit: function($form) {
    var email = document.getElementById("email-feca4800-6b73-4a38-8f80-1406a1b726b7").value;
    sessionStorage.setItem("email",email);
}

在另一页上:

setTimeout('Redirect()', 5000);
function Redirect()
{
    var email = sessionStorage.getItem("email");
    window.location="https://myurl.com/login?req=md-freetier-signup&src="+email;
}

如果您在此之后再也不使用会话变量,请确保使用sessionStorage.clear();将其删除sessionStorage.clear();

您可以尝试在JS的localstorage中传递变量:

localStorage.setItem('email', 'your@email.fr');

然后你可以找回来

localStorage.getItem('email');

据我了解,这是一个上下文问题。 替换为var email = document.getElementById("email-feca4800-6b73-4a38-8f80-1406a1b726b7").value; 通过window.email = document.getElementById("email-feca4800-6b73-4a38-8f80-1406a1b726b7").value; window.location="https://myurl.com/login?req=md-freetier-signup&src="+window.email; 应该管用。

这是一个可行的解决方案,尽管不是一个好的解决方案! 尽可能避免使用“窗口”对象,并始终考虑使用命名空间,但我不知道HubSpot允许您使用多少。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM