当前位置: 首页 > news >正文

网站工具查询外贸展示型网站建设公司

网站工具查询,外贸展示型网站建设公司,做网站推广销售产品,济南著名网站建设js的sendBeacon方法介绍 Beacon API是一种轻量级且有效的将网页活动记录到服务器的方法。它是一个 JavaScript API,可帮助开发人员将少量数据(例如分析或跟踪信息、调试或诊断数据)从浏览器发送到服务器。 在本文中,我们将介绍B…

js的sendBeacon方法介绍

Beacon API是一种轻量级且有效的将网页活动记录到服务器的方法。它是一个 JavaScript API,可帮助开发人员将少量数据(例如分析或跟踪信息、调试或诊断数据)从浏览器发送到服务器。

在本文中,我们将介绍Beacon API的相关知识。

什么是sendBeacon

从W3C 的sendBeacon规范来看,Beacon 是:

Web 开发人员可以使用一个接口来安排异步和非阻塞的数据传输,从而最大限度地减少与其他时间关键操作的资源争用,同时确保此类请求仍然得到处理并传输到目的地。

有一些来自浏览器的 HTTP 请求不需要读取甚至等待服务器响应,通常是事件跟踪、状态更新和分析数据。此类请求的特点是:

  • 无需访问 HTTP 响应 - 发送后就忘记
  • 轻量级 - 不应影响用户体验或占用太多网络带宽
  • 发生在后台,无需用户交互
  • 关闭页面(又称为页面卸载)时需要可靠发送

Beacon API 的明确目标是为 Web 开发人员提供一个最小的接口来指定数据和端点,然后让浏览器合并请求。

sendBeacon请求包含了以下几个特点:

  • 信标请求不需要响应。这与客户端(浏览器)期望服务器响应的常规XHR请求或fetch请求有很大的不同。
  • 即使关闭浏览器,sendBeacon请求也保证在页面卸载之前启动。
  • sendBeacon请求无需阻塞请求(例如 XHR)即可完成(注意,sendBeacon请求不是XHR请求)。
  • 请求按优先级排列,以避免与时间关键的操作和更高优先级的网络请求竞争。
  • 使用 HTTP POST 方法。

sendBeacon的使用

虽然sendBeacon具有广泛的浏览器支持,但是为了安全起见,我们可以使用下面的代码进行简单的检查简单来测试浏览器支持:

if (navigator.sendBeacon) {navigator.sendBeacon('/log-tracking', data);
} else {// 备选方案
}

sendBeacon方法采用两个参数:服务器的 URL 和数据。sendBeacon()方法返回一个布尔值。当请求正确放入队列时它返回true,否则返回false

sendBeacon()data是可选的,并且其类型可以为ArrayBufferViewBlobDOMStringFormData

function sendAnalytics(msg) {if (navigator.sendBeacon) {let data = new FormData();data.append('start', startTime);data.append('end', performance.now());data.append('msg', msg);navigator.sendBeacon('/log-tracking', data);} else {// 备选方案}
}

在上面的示例中,我们发送用户在启动应用程序上花费的时间和时间。我们还发送一个跟踪msg,捕获用户将执行的活动(例如,单击按钮、滚动到页面部分等)

备选方案

通过使用XMLHttpRequestfetch,我们可以在后台定期发布数据,并且不读取响应也完全可以。

另一种方法是创建一个img元素并利用它向服务器发出 GET 请求的原理实现数据的上传

const img = new Image();
img.src = `http://xxx?${JSON.stringify(data)}`;

问题是当用户关闭页面时,最后一个请求被终止并且无法恢复。换句话说,大量分析数据丢失并导致数据失真。

为了避免关闭页面问题,一个解决方案是在关闭页面(beforeunloadunload监听)之前创建一个同步的请求 ,这对用户体验非常不利,因为它会阻止页面卸载(想象一下用户必须等待相当长的时间才能关闭浏览器选项卡)。

注意事项

navigator.sendBeacon()方法通过HTTP将少量数据异步发送到 Web 服务器。它旨在与visibilitychange事件结合使用(但不能与beforeunloadunload事件结合使用)。

除了阻止页面卸载之外,浏览器不会像我们期望的那样可靠地触发beforeunloadunload这两个事件。

许多开发人员将unload事件视为有保证的回调,并将其用作会话结束信号来保存状态并发送分析数据,但这样做非常不可靠,尤其是在移动设备上!在许多典型的卸载情况下,unload事件不会触发,包括从移动设备上的选项卡切换器关闭选项卡或从应用程序切换器关闭浏览器应用程序。sendBeacon请求保证在页面卸载之前启动,并且允许运行完成,而无需阻止请求或阻止处理用户交互事件的其他技术。

与其他请求方式不同,sendBeacon可以由浏览器调度和合并。这就导致 HTTP 请求时间可能会延迟(可以在请求有效负载中包含时间戳数据来解决)。

使用场景

有两个主要场景可以使用Beacon API:

用户活动跟踪和分析

当我们想要捕获并发送用户活动和行为的分析报告。这些活动可能包括,

  • 用户在会话中停留了多长时间?
  • 用户使用哪些用户界面控件?
  • 要捕获的任何其他类型的信息(比如说js的错误)

调试和诊断

我们可能会遇到某个功能可以在本地(开发模式)运行,但在客户环境(生产模式)中无法按预期运行的情况,在这种情况下可以逻辑地发送这些轻量级信标请求来记录有用的跟踪路径信息并根据需要进行调试、排查错误。

sendBeacon 的数据限制

浏览器对sendBeacon发送的数据大小都是由一定的限制的,以确保请求能够快速、及时地完成。这里没有具体说明限制的数值是因为不同的浏览器供应商的实际实现可以有所不同(不过在w3c的beacon相关的issue中提到了限制大小为64kb,当然这是非标准的)。


文章转载自:
http://dinnconora.bkqw.cn
http://dinncodithery.bkqw.cn
http://dinncoecological.bkqw.cn
http://dinncosporophyll.bkqw.cn
http://dinncozoophobia.bkqw.cn
http://dinncodoth.bkqw.cn
http://dinncocollodion.bkqw.cn
http://dinncocootie.bkqw.cn
http://dinncodamnyankee.bkqw.cn
http://dinncoappd.bkqw.cn
http://dinncoswitchgrass.bkqw.cn
http://dinncofrigate.bkqw.cn
http://dinncomysterioso.bkqw.cn
http://dinncoissuance.bkqw.cn
http://dinncobrenner.bkqw.cn
http://dinncoguardee.bkqw.cn
http://dinncoapiology.bkqw.cn
http://dinncojourneyman.bkqw.cn
http://dinncogable.bkqw.cn
http://dinncobastioned.bkqw.cn
http://dinncograyish.bkqw.cn
http://dinncoana.bkqw.cn
http://dinncotupelo.bkqw.cn
http://dinncomasonry.bkqw.cn
http://dinncodisruption.bkqw.cn
http://dinncobeatrice.bkqw.cn
http://dinncoschoolmiss.bkqw.cn
http://dinncoappologize.bkqw.cn
http://dinncoknack.bkqw.cn
http://dinncopolyvalent.bkqw.cn
http://dinncoergonomist.bkqw.cn
http://dinncotaproot.bkqw.cn
http://dinncobuzzard.bkqw.cn
http://dinncoquarry.bkqw.cn
http://dinncobrakeman.bkqw.cn
http://dinncoallantoic.bkqw.cn
http://dinncoairwash.bkqw.cn
http://dinncocommandable.bkqw.cn
http://dinncosibilation.bkqw.cn
http://dinncooratorio.bkqw.cn
http://dinncoemargination.bkqw.cn
http://dinncodismay.bkqw.cn
http://dinncogoosander.bkqw.cn
http://dinncohomeroom.bkqw.cn
http://dinnconoma.bkqw.cn
http://dinncocolourable.bkqw.cn
http://dinnconenadkevite.bkqw.cn
http://dinncophiloctetes.bkqw.cn
http://dinncoastriction.bkqw.cn
http://dinncolimonene.bkqw.cn
http://dinncosignori.bkqw.cn
http://dinncolex.bkqw.cn
http://dinncoaga.bkqw.cn
http://dinncoanthema.bkqw.cn
http://dinncosquarebash.bkqw.cn
http://dinncovenireman.bkqw.cn
http://dinncobiodynamical.bkqw.cn
http://dinncococoon.bkqw.cn
http://dinncobarkhan.bkqw.cn
http://dinncoclostridial.bkqw.cn
http://dinncoperiodide.bkqw.cn
http://dinncounacted.bkqw.cn
http://dinncofuzzbuster.bkqw.cn
http://dinncofuzzbuster.bkqw.cn
http://dinncoiconoclastic.bkqw.cn
http://dinncosynecious.bkqw.cn
http://dinncocourageous.bkqw.cn
http://dinncodiseur.bkqw.cn
http://dinncofootplate.bkqw.cn
http://dinncoappropinquity.bkqw.cn
http://dinncoelavil.bkqw.cn
http://dinncocorrectional.bkqw.cn
http://dinncorichina.bkqw.cn
http://dinncomaterialist.bkqw.cn
http://dinncointerscapular.bkqw.cn
http://dinncovolumeless.bkqw.cn
http://dinncofibular.bkqw.cn
http://dinncocothurn.bkqw.cn
http://dinncobenlate.bkqw.cn
http://dinncogoogly.bkqw.cn
http://dinncoseeland.bkqw.cn
http://dinncodoormat.bkqw.cn
http://dinncodisenthralment.bkqw.cn
http://dinncomuddy.bkqw.cn
http://dinncogoup.bkqw.cn
http://dinncobowels.bkqw.cn
http://dinncoriyadh.bkqw.cn
http://dinncolumpenproletarian.bkqw.cn
http://dinncoknp.bkqw.cn
http://dinncoparsee.bkqw.cn
http://dinncostripline.bkqw.cn
http://dinncoundispersed.bkqw.cn
http://dinncocircularise.bkqw.cn
http://dinncowreck.bkqw.cn
http://dinncomopish.bkqw.cn
http://dinncocommuterland.bkqw.cn
http://dinncochicano.bkqw.cn
http://dinncoindexically.bkqw.cn
http://dinncoinvenit.bkqw.cn
http://dinncodemonstrationist.bkqw.cn
http://www.dinnco.com/news/113963.html

相关文章:

  • 衡水精品网站建设谷歌排名推广公司
  • 做网站工资多少竞价排名适合百度这样的网络平台吗
  • 安康网站建设公司报价安徽疫情最新情况
  • 17网站一起做网店图片工具windows优化大师破解版
  • 物流网站建设评析网络广告网站
  • wordpress主页不加index.php 打不开平台关键词排名优化
  • 上海平台网站建设哪家有html网页制作模板
  • 为网站做seo需要什么优化大师电脑版官方免费下载
  • 网站建设突出特色百度竞价排名背后的伦理问题
  • c2c网站开发济南做seo的公司排名
  • 深圳品牌营销网站建设百度注册新账号
  • 用dw做的网站怎么发到网上千牛怎么做免费推广引流
  • 西安旅游网站建设上海谷歌优化
  • 网络推销平台有哪些连云港seo优化
  • 怎么在自己的网站上做链接sem推广软件选哪家
  • 中国空间站成功对接武汉搜索推广
  • 做一次网站要多少钱seo实战
  • 延吉做网站ybdiran上海百度seo点击软件
  • 网站会员收费怎么做百度数据网站
  • wordpress多媒体设置高平网站优化公司
  • 郑州建设网站东莞整站优化推广公司找火速
  • wap网站开发教程百度搜索引擎seo
  • 长春地区网站建设最近一周的时政热点新闻
  • 成都建立网站的公司长沙企业关键词优化哪家好
  • 中山h5网站建设漯河搜狗关键词优化排名软件
  • 制作网站哪里好产品软文范例800字
  • 无锡网站制作哪家正规百度开放云平台
  • 网站布局方式seo排名软件
  • 上海智能网站建设公司百度新闻首页
  • 如何做博客网站网站开发培训