简体   繁体   English

Javascript:如何在代码块之间传输变量?

[英]Javascript: How to transfer variables between code blocks?

I am using Mautic which is an email newsletter software.我正在使用 Mautic,这是一个电子邮件通讯软件。 Users enter their details for the newsletter via HTML landing pages that contains a simple HTML form.用户通过包含简单 HTML 表单的 HTML 登录页面输入新闻通讯的详细信息。

This JS code allows Mautic to process the data from the HTML landing page.此 JS 代码允许 Mautic 处理来自 HTML 登录页面的数据。 It is placed right before the closing body tag of my HTML page:它位于我的 HTML 页面的结束正文标记之前:

Mautic Code Block Mautic 代码块

<script>
    (function(w,d,t,u,n,a,m){w['MauticTrackingObject']=n;
        w[n]=w[n]||function(){(w[n].q=w[n].q||[]).push(arguments)},a=d.createElement(t),
        m=d.getElementsByTagName(t)[0];a.async=1;a.src=u;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://myurl/mtc.js','mt');
       mt('send', 'pageview', {'tags':'landing-page-name'});

</script>

The last line 'tags':'landing-page-name' tells Mautic from which landing page the inputted data has come from.最后一行'tags':'landing-page-name'告诉 Mautic 输入的数据来自哪个着陆页。 At the moment, landing-page-name has to be hardcoded in. I would prefer it if landing-page-name could be dynamically replaced with the file name of the HTML page.目前, landing-page-name必须是硬编码的。如果landing-page-name可以动态替换为HTML 页面的文件名,我会更喜欢它。

On my HTML page, I already have a Javascript block that extracts the file name of the current HTML page and inserts it into a field on my Mautic form (on the HTML landing page).在我的 HTML 页面上,我已经有一个 Javascript 块,用于提取当前 HTML 页面的文件名并将其插入到我的 Mautic 表单(在 HTML 登录页面上)的字段中。 It assigns the name of the HTML page to a variable called val .它将 HTML 页面的名称分配给名为val的变量。

HTML Name Code Block HTML 名称代码块

<script>
    window.onload = function() {
        let val = location.pathname.split('/').pop().replace('.html', '');
        val = val.replace(/-/g, ' ');
        val = val.replace(/(^\w{1})|(\s{1}\w{1})/g, match => match.toUpperCase());
        document.getElementById('mauticform_input_genericcouponclub_book_purchased').value = val;
        document.getElementById('mauticform_genericcouponclub_book_purchased').style.display = "none";
    }
</script>  

This code block is directly placed above the Mautic Code Block.此代码块直接放置在 Mautic 代码块上方。

How can I pass the val variable into the Mautic Code Block, so I can use in the 'tags': part rather than having to hardcode the name?如何将val变量传递到 Mautic 代码块中,以便我可以在 'tags': 部分中使用而不必对名称进行硬编码?

Add filename to global/window object.将文件名添加到全局/窗口对象。 Access the same variable in other functions.在其他函数中访问相同的变量。

Make sure you wrap second function(consumer) in windows.load as well because file name will be undefined before that.确保在windows.load中也包装了第二个函数(消费者),因为在此之前文件名将是未定义的。 Alternatively you can have a fallback name.或者,您可以有一个后备名称。

 <script> window.onload = function() { let val = location.pathname.split('/').pop().replace('.html', ''); val = val.replace(/-/g, ' '); val = val.replace(/(^\\w{1})|(\\s{1}\\w{1})/g, match => match.toUpperCase()); document.getElementById('mauticform_input_genericcouponclub_book_purchased').value = val; document.getElementById('mauticform_genericcouponclub_book_purchased').style.display = "none"; // attach name to window/global object window.htmlFileName = val; } </script> ----------------------- <script> window.onload = function() { var fileName = window.htmlFileName || 'landing-page-name' (function(w,d,t,u,n,a,m){w['MauticTrackingObject']=n; w[n]=w[n]||function(){(w[n].q=w[n].q||[]).push(arguments)},a=d.createElement(t), m=d.getElementsByTagName(t)[0];a.async=1;a.src=u;m.parentNode.insertBefore(a,m) })(window,document,'script','https://myurl/mtc.js','mt'); mt('send', 'pageview', {'tags': fileName }); } </script>

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

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