因此,我正在从事这个项目,并且我试图根据身份验证状态隐藏/显示一些元素。我设法使所有其他元素按我的要求工作,但是我对一个特定的元素有问题。我有一个上传表单,仅当用户登录时才想显示。这是我的表格:
<form id="addVideo" class="uploadForm">
<h3>Alege fisier:</h3>
<input id="fileUpload" type="file" required>
<input id="caption" type="text" required>
<button id="buttonSubmit" onclick="uploadFile()">Submit </button>
</form>
这是我的JS:
const btnLogin = document.getElementById('modalBtn');
const btnLogout = document.getElementById('logout');
const contentUploader = document.getElementById('addVideo');
const btnAcasa = document.getElementById('acasa');
const btnBiblioteca = document.getElementById('biblioteca');
const setUI = (user)=>{
if(user){
btnLogin.style.display='none';
btnLogout.style.display='block';
if(window.location == 'biblioteca.html'){
contentUploader.style.display ='block';
}
btnAcasa.style.display='block';
btnBiblioteca.style.display ='block';
}
else{
btnLogin.style.display = 'block';
btnLogout.style.display = 'none';
if(window.location === 'biblioteca.html'){
contentUploader.style.display = 'none';
}
btnAcasa.style.display ='block';
btnBiblioteca.style.display ='block';
}
}我使用window.location,因为我在2个不同的页面上运行了此脚本。这是我如何调用函数:
auth.onAuthStateChanged(user =>{
if(user){
setUI(user);
console.log(user);
}
else{
setUI()
console.log(user);
}
})
Window.location
返回Location对象,该对象不等于字符串。请参阅https://developer.mozilla.org/en-US/docs/Web/API/Window/location
您可以使用
if(window.location.href == 'biblioteca.html'){
contentUploader.style.display ='block';
}
但是这里您还需要考虑域名以及biblioteca.html
之前的所有内容