1
querySelectorAll works normally in Firefox, but not in Chrome. I wonder if anyone can help me in such a matter, follow the code.
<script>
document.querySelectorAll('a').forEach(link => {
let conteudo = document.getElementById('conteudo')
link.onclick = function (e) {
e.preventDefault()
fetch(link.href)
.then(resp => resp.text())
.then(html => conteudo.innerHTML = html)
}
})
</script>
In the console appears the following error:
Uncaught (in Promise) Typeerror: Failed to fetch at Htmlanchorelement.link.onclick
HTML
<body data-spy="scroll" data-target=".navbar-collapse">
<nav class="navbar navbar-inverse" style="background-color: #000080; height: 40px; font: bold 15px Arial, sans-serif;">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#" style = "font: italic 20px Verdana;">KAUYYZI</a>
<button class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<i class="fa fa-user-md"></i>
<span class="icon icon-bar">A</span>
<span class="icon icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse">
<ul class = "nav navbar-nav">
<li>
<a href="tabela.html">Contato</a>
</li>
<li>
<a href="agendamentoPaciente.html">Agendamento</a>
</li>
</ul>
</div>
</div>
</nav>
<section id="conteudo"></section>
<footer class = "ftr">
KAUYYZI 2018 ©
</footer>
Network Error
Scheme URL must be "http" or "https" for CORS request.
Gabriel, what gives you
console.log(document.querySelectorAll('a').length
);`? It would be interesting to join the HTML that gives you this problem...– Sergio
Your code seems to work normally on Chrome: https://jsfiddle.net/zwz9kmvn/
– bfavaretto
What is the address you try to access in fetch? If it’s from another domain, the browser will block access.
– bfavaretto
@bfavaretto as the Web Site is under construction, is in WAMP.
– Gabriel
@Gabriel but which URL is on the link that clicked that caused the failure, may be cross-origin problem
– Guilherme Nascimento
@Gabriel posts the links used on
<a href>
please. So we can help.– Guilherme Nascimento
@Guilhermenascimento the problem that works on Firefox and Chrome does not...
– Gabriel
Go to the console again, but in the network tab (if your browser is in Portuguese the tab is called Network), click on it, then go back to your site and click on any link, then go back to the network tab console window, see the HTTP requests, the one you clicked from the link must have some error. Let us know the error, this should help solve your problem.
– Guilherme Nascimento
You knew, it was CORS error (cross origin). So now say, which link exactly caused the error
URL scheme must be "http" or "https" for CORS request.
?– Guilherme Nascimento
@Guilhermenascimento I’m testing locally, would that generate error? something like this would be, file://C:/XXX/XXX%20XXX/Desktop/ex/scheduling.html.
– Gabriel
So that’s it. It will only work on the HTTP protocol.
– Sam
Gabriel, finish the question by marking in one of the answers. Do not leave the question open because it is not interesting.
– Sam