-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
30 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,21 @@ | ||
import isFunction from './isFunction' | ||
|
||
let isInBrowser: boolean | undefined | ||
|
||
/** | ||
* 是否在浏览器环境中。 | ||
* 检查是否在浏览器环境中。 | ||
* | ||
* @param [callback] 在浏览器环境中执行的回调 | ||
* @returns 是(true)或否(false) | ||
*/ | ||
export default typeof window === 'object' | ||
&& typeof document === 'object' | ||
&& document.nodeType === 9 | ||
export default function inBrowser(callback?: () => void): boolean { | ||
if (isInBrowser === undefined) { | ||
isInBrowser = typeof window === 'object' | ||
&& typeof document === 'object' | ||
&& document.nodeType === 9 | ||
} | ||
if (isFunction(callback) && isInBrowser) { | ||
callback() | ||
} | ||
return isInBrowser | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters