我写了这个简单的函数一次,以显示对哈希变更通知中:使用if-if或的if-else if-else语句的简单功能
function watchHash() {
if(location.hash == '#thanks') {
displayNotification('Thanks for your feedback, I\'ll try to get back to you as soon as possible.'); // Notify on form submit success
}
if(location.hash == '#error') {
displayNotification('Oops, something went wrong ! Please try again.'); // Notify on form submit error
}
}
window.onhashchange = watchHash;
我今天回来吧,我想,是它正确如果我这样写呢?
function watchHash() {
if(location.hash == '#thanks') {
displayNotification('Thanks for your feedback, I\'ll try to get back to you as soon as possible.'); // Notify on form submit success
}
else if(location.hash == '#error') {
displayNotification('Oops, something went wrong ! Please try again.'); // Notify on form submit error
}
else {
return;
}
}
window.onhashchange = watchHash;
如果是这样,是不是相关?
我在这里有点困惑,我想坚持最佳实践。
感谢您的帮助。
请注意,在所有浏览器中不支持hashchange事件(正确),所以你应该填充(如果你还没有) – m90
是的,我知道这一点。 – morgi