首页 > 其他 > 详细

检测是否是标准浏览器环境 函数

时间:2020-07-11 12:52:22      阅读:45      评论:0      收藏:0      [点我收藏+]

实现思路:根据当前环境是否存在某个对象来判别,如果光存在还不足以判别就再判断属性,最终将结果返回。

	/**
	 * Determine if we‘re running in a standard browser environment
	 *
	 * This allows axios to run in a web worker, and react-native.
	 * Both environments support XMLHttpRequest, but not fully standard globals.
	 *
	 * web workers:
	 *  typeof window -> undefined
	 *  typeof document -> undefined
	 *
	 * react-native:
	 *  navigator.product -> ‘ReactNative‘
	 * nativescript
	 *  navigator.product -> ‘NativeScript‘ or ‘NS‘
	 */
	function isStandardBrowserEnv() {
	  if (typeof navigator !== ‘undefined‘ && (navigator.product === ‘ReactNative‘ ||
	                                           navigator.product === ‘NativeScript‘ ||
	                                           navigator.product === ‘NS‘)) {
	    return false;
	  }
	  return (
	    typeof window !== ‘undefined‘ &&
	    typeof document !== ‘undefined‘
	  );
	}

代码来源:axios源码
技术分享图片

检测是否是标准浏览器环境 函数

原文:https://www.cnblogs.com/xiaolantian/p/13283060.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!