forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDOMUtils.js
More file actions
60 lines (52 loc) · 1.84 KB
/
DOMUtils.js
File metadata and controls
60 lines (52 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
export var canUseDOM = !!(
typeof window !== 'undefined' && window.document && window.document.createElement
);
export function addEventListener(node, type, listener) {
if (node.addEventListener) {
node.addEventListener(type, listener, false);
} else {
node.attachEvent('on' + type, listener);
}
}
export function removeEventListener(node, type, listener) {
if (node.removeEventListener) {
node.removeEventListener(type, listener, false);
} else {
node.detachEvent('on' + type, listener);
}
}
export function getHashPath() {
// We can't use window.location.hash here because it's not
// consistent across browsers - Firefox will pre-decode it!
return window.location.href.split('#')[1] || '';
}
export function replaceHashPath(path) {
window.location.replace(
window.location.pathname + window.location.search + '#' + path
);
}
export function getWindowPath() {
return window.location.pathname + window.location.search;
}
export function getWindowScrollPosition() {
return {
x: window.pageXOffset || document.documentElement.scrollLeft,
y: window.pageYOffset || document.documentElement.scrollTop
};
}
export function setWindowScrollPosition(scrollX, scrollY) {
window.scrollTo(scrollX, scrollY);
}
/**
* taken from modernizr
* https://github.com/Modernizr/Modernizr/blob/master/LICENSE
* https://github.com/Modernizr/Modernizr/blob/master/feature-detects/history.js
* changed to avoid false negatives for Windows Phones: https://github.com/rackt/react-router/issues/586
*/
export function supportsHistory() {
var ua = navigator.userAgent;
if ((ua.indexOf('Android 2.') !== -1 || ua.indexOf('Android 4.0') !== -1) && ua.indexOf('Mobile Safari') !== -1 && ua.indexOf('Chrome') === -1 && ua.indexOf('Windows Phone') === -1) {
return false;
}
return window.history && 'pushState' in window.history;
}