When I study the source code of vue-router, I find that, it uses location.assign('xxx') to change location, not the way I always use location.href = 'xxx'. The source code is here, and I've pasted it below:
export function pushState (url?: string, replace?: boolean) {
saveScrollPosition()
// try...catch the pushState call to get around Safari
// DOM Exception 18 where it limits to 100 pushState calls
const history = window.history
try {
if (replace) {
// preserve existing history state as it could be overriden by the user
const stateCopy = extend({}, history.state)
stateCopy.key = getStateKey()
history.replaceState(stateCopy, '', url)
} else {
history.pushState({ key: setStateKey(genStateKey()) }, '', url)
}
} catch (e) {
window.location[replace ? 'replace' : 'assign'](url)
}
}
I want to find out why they prefer location.assign() to location.href, after googling around, it turns out that there is only one security difference between them.
- The
location.assign()has some security limits
So, I still don't understand why the vue-router team choose location.assign() over location.href.
Here are some related articles: