mirror of
https://github.com/nuxt/nuxt.git
synced 2025-03-09 03:03:18 +00:00
fix DOMException when using an url-hash that is not valid as css selector
This commit is contained in:
parent
03f89421c2
commit
5f55bf76bd
@ -53,9 +53,22 @@ const scrollBehavior = function (to, from, savedPosition) {
|
||||
window.$nuxt.$once('triggerScroll', () => {
|
||||
// coords will be used if no selector is provided,
|
||||
// or if the selector didn't match any element.
|
||||
if (to.hash && document.querySelector(to.hash)) {
|
||||
// scroll to anchor by returning the selector
|
||||
position = { selector: to.hash }
|
||||
if (to.hash) {
|
||||
let hash = to.hash
|
||||
// CSS.escape() is not supported with IE and Edge.
|
||||
// But we can use the following polyfill to solve this problem.
|
||||
// https://github.com/mathiasbynens/CSS.escape
|
||||
if (typeof window.CSS !== 'undefined' && typeof window.CSS.escape !== 'undefined') {
|
||||
hash = '#' + window.CSS.escape(hash.substr(1))
|
||||
}
|
||||
try {
|
||||
if (document.querySelector(hash)) {
|
||||
// scroll to anchor by returning the selector
|
||||
position = { selector: hash }
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('failed to save the scroll position. possibly this problem can be solved using a polyfill for CSS.escape().')
|
||||
}
|
||||
}
|
||||
resolve(position)
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user