在两个不同的页面中使用相同的重定向函数



好的,我的索引页有这个标题和登录选项,我选择了使用这两个函数的按钮

function getHeaderButtons() {
const showHidePopUp = document.querySelector('.titleAndMenuIcon > img')
const windowPopUP = document.querySelector('.mobilePopUp')
const main = document.querySelector('main')

showHidePopUp.addEventListener('click', () => {
windowPopUP.classList.toggle('moveUpAndDown')
console.log('ok')

})
}
function getLoginAndSubscribeButtons() {
const login = document.querySelector('.mobilePopUp__login')
const subscribe = document.querySelector('.mobilePopUp__subscribe')
login.addEventListener('click', () => {
window.location.href = "./src/pages/loginPage.html"
})
subscribe.addEventListener('click', () => {
window.location.href = './src/pages/subscribePage.html'
})
}

和这些都工作得很好,在我的主页,问题开始时,我导出这些功能到其他页面(所以我不必重写一遍)

这是显示的信息在这里输入图像描述

我想要一种方式,即使在订阅页面中单击订阅按钮,它也会再次重定向到订阅页面。有什么办法吗?

我试图查找一个代表主页的通配符,以便它总是指向我的索引页

使用绝对路径

/src/pages/loginPage.html

问题:您正在使用相对路径,这意味着文件URL将基于CURRENT位置。

相对的例子:


CURRENT PAGE: domain.com/index.html
window.location.href = "./src/pages/loginPage.html"
FINAL URL: /src/pages/loignPage.html
CURRENT PAGE: domain.com/blogs/pets.html
window.location.href = "./src/pages/loginPage.html"
FINAL URL: /blogs/src/pages/loignPage.html

解决方案:使用绝对

window.location.href = "/src/pages/loginPage.html"

最新更新