AMP设置并获取Cookie值 [英] AMP set and get cookies values

查看:126
本文介绍了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中的实现逻辑

  1. 用户输入电子邮件地址后,我们将值存储在cookie中.
  2. 如果用户第二次基于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值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆