When i do a document.location.href to go to another HTML page and then try to modify elements in the new page (which I would use getElementById for), I get that the element is not found in the webpage. I think it is because the new page is not fully loaded yet and it is looking in the previous HTML page.. Any idea how I can fix this issue? For example:
async function navigateToAccount(accId, userStatus, userBalance) {
//fetch account information
try {
const token = localStorage.getItem('token')
const response = await fetch(
'someapi/api/getAccountDetails/' + accId,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
}
)
const data = await response.json()
if (data.status == false) {
throw new Error(data.message)
}
window.location.href = 'accountPage.html'
//the below is not executing
const accountName = document.getElementById('accountPageName')
accountName.textContent = localStorage.getItem('name')
const accountBalance = document.getElementById('accountPageBalance')
accountBalance.textContent = userBalance
const ccNumber = document.getElementById('accountPageccNumber')
ccNumber.textContent = data.account.creditCard.number
const ccName = document.getElementById('accountPageccName')
ccName.textContent = localStorage.getItem('name')
const ccExp = document.getElementById('accountPageccExp')
ccExp.textContent = data.account.creditCard.expiryDate
const ccCvv = document.getElementById('accountPagecccvv')
ccCvv.textContent = data.account.creditCard.cvv
} catch (err) {
console.log(err.message)
}
}
I would get an error saying that element with Id accountPageName is not found.