AMP设置并获取Cookie值

编程入门 行业动态 更新时间:2024-10-17 17:21:17
本文介绍了AMP设置并获取Cookie值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是这个AMP的新手.在网络中,我会遇到以下情况.

I am new in this AMP. In web I have scenario like below.

示例我有一个包含100个段落内容的页面...对于用户来说,第一次访问仅显示10个段落内容的页面.然后会以输入形式询问用户的电子邮件地址.用户提供电子邮件地址后,将显示剩余的90段内容...同一用户第二次访问该页面时,我们在不询问电子邮件的情况下显示了内容.

Example I have a page with 100 paragraphs content ... For the user first time visit the page displaying only 10 paragraphs of content. then will ask to user email address in input form. after user provide the email address then will display remaining 90 paragraphs content... The same user visit 2nd time that page we displayed the content without asking email.

WEB中的实现逻辑

  • 用户输入电子邮件地址后,我们将值存储在cookie中.
  • 如果用户第二次基于Cookie值访问,我们将显示内容.
  • 因此需要相同的逻辑来实现AMP页面.

    So same logic needs to implements the AMP pages.

    我在放大器和其他东西中设计表单,但是努力设置cookie值.

    I design the form in amp and other stuff but struggling to set the cookie values..

    我在WEB中使用了以下代码:

    The following code I am used in WEB:

    function setCookie(name,value,days) { var expires = ""; if (days) { var date = new Date(); date.setTime(date.getTime() + (days*24*60*60*1000)); expires = "; expires=" + date.toUTCString(); } document.cookie = name + "=" + (value || "") + expires + "; path=/"; } function getCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; }

    一旦用户输入电子邮件并提交表格

    Once user enter the email and submit the form

    setCookie('article-page','email','XXXXX');

    推荐答案

    您不能直接从AMP页面中访问cookie.但是,您可以使用 amp-access 组件在服务器端实现此行为.

    You can't directly access cookies from within an AMP page. However, you can use the amp-access component to implement this behavior server-side.

    您可以忽略amp-access提供的登录/注销功能.您唯一需要做的就是更改对授权端点的响应根据用户是否提供了电子邮件地址,返回true或false.根据此信息,您可以调整页面上显示的内容.

    You can ignore the login/logout features provided by amp-access. The only thing you need to do is to change the response for the authorization endpoint to either return true or false depending on whether the user has provided an email address. Based on this information you can then adjust the content that is displayed on the page.

    更多推荐

    AMP设置并获取Cookie值

    本文发布于:2023-10-08 01:22:33,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1471100.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:AMP   Cookie

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!