The following code should work as explained:
Links in the article should see its target attribute removed (so it displays in the browser and not the iframe) but it doesn’t. I make sure the window (with developper’s tool) is the appropriate size, reload with ctrl+r, and it doesn’t change.
I wish for this:
<a href="/glossaire/une-demarche-pionniere/" onclick="document.querySelector("iframe").removeAttribute("hidden")" target="iframe_glossary">démarche pionnière</a>
to loose its target attribute.
I tried:
<script>
if (screen.availWidth <= 604) {
document.querySelector('p a').removeAttribute('target');
}
</script>
Edit:
Page structure:
<html lang="fr">
<head>
<script src="/livereload.js?mindelay=10&v=2&port=1313&path=livereload" data-no-instant="" defer=""></script>
<script>$(window).Width<=604&&document.querySelector("p a").removeAttribute("target")</script></head>
<body class="page-template-default page">
<header id="header"><a href="http://localhost:1313/" id="logo"></a></header>
<div id=content>
<main id="page" class="page-content">
<article class="page-post">
<div class="post-ctt">
<p>C’est une
<a href="/glossaire/une-demarche-pionniere/" onclick="document.querySelector("iframe").removeAttribute("hidden")" target="iframe_glossary">démarche pionnière</a>
<iframe hidden="" name="iframe_glossary" allow-same-origin="" seamless=""></iframe>
<button onclick="document.querySelector("iframe").setAttribute("hidden","hidden")">☒</button>
</div>
</article>
</main>
</div>
</body>
</html>
2
Answers
screen.availWidth
doesn not work when you manually resize browser size. It works when tried with difference device sizes (try responsive feature on dev tools)To make this work, try
window.innerWidth
. It gives correct width when browser/window is resized.I would check the innerWidth not the screen’s entire available width. also your script runs before the content is there, try waiting for DOMContentLoaded
To handle more than one element, do
document.querySelectorAll
and forEach over it