Skip to content

Browser default actions #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
When the browser reads the `on*` attribute like `onclick`, it creates the handler from its content.
Lorsque le navigateur lit l'attribut `on*`, comme `onclick`, il créer le gestionnaire depuis son contenu.

For `onclick="handler()"` the function will be:
Pour `onclick="handler()"` la fonction sera:

```js
function(event) {
handler() // the content of onclick
handler() // le contenu de onclick
}
```

Now we can see that the value returned by `handler()` is not used and does not affect the result.
Maintenant nous pouvons voir que la valeur retournée par `handler()` n'est pas utilisée et n'affecte pas le résultat.

The fix is simple:
La correction est simple:

```html run
<script>
Expand All @@ -23,7 +23,7 @@ The fix is simple:
<a href="https://w3.org" onclick="*!*return handler()*/!*">w3.org</a>
```

Also we can use `event.preventDefault()`, like this:
Nous pouvons aussi utiliser `event.preventDefault()`, comme ceci:

```html run
<script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 3

---

# Why "return false" doesn't work?
# Pourquoi "return false" ne fonctionne pas?

Why in the code below `return false` doesn't work at all?
Pourquoi dans le code ci-dessous `return false` ne fonctionne pas?

```html autorun run
<script>
Expand All @@ -14,9 +14,9 @@ Why in the code below `return false` doesn't work at all?
}
</script>

<a href="https://w3.org" onclick="handler()">the browser will go to w3.org</a>
<a href="https://w3.org" onclick="handler()">le navigateur va aller sur w3.org</a>
```

The browser follows the URL on click, but we don't want it.
Le navigateur suit le lien lors du clic, mais nous ne voulons pas ça.

How to fix?
Comment réparer ce problème?
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
That's a great use of the event delegation pattern.
C'est une bonne utilisation de la délégation d'évènement.

In real life instead of asking we can send a "logging" request to the server that saves the information about where the visitor left. Or we can load the content and show it right in the page (if allowable).
Dans la vie réelle au lieu de demander nous pouvons envoyer une requête de "logging" au serveur pour sauvegarder les informations sur où l'utilisateur a quitté. Ou nous pouvons charger le contenu et l'afficher directement dans la page (si permis).

All we need is to catch the `contents.onclick` and use `confirm` to ask the user. A good idea would be to use `link.getAttribute('href')` instead of `link.href` for the URL. See the solution for details.
Tout ce que nous avons à faire est de capturer le `contents.onclick` et utiliser `confirm` pour demander à l'utilisateur. Une bonne idée serait d'utiliser `link.getAttribute('href')` plutôt que `link.href` pour l'URL. Regardez la solution pour plus de détails.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
contents.onclick = function(event) {

function handleLink(href) {
let isLeaving = confirm(`Leave for ${href}?`);
let isLeaving = confirm(`Quitter pour ${href}?`);
if (!isLeaving) return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ importance: 5

---

# Catch links in the element
# Capturer des liens dans l'élément

Make all links inside the element with `id="contents"` ask the user if they really want to leave. And if they don't then don't follow.
Faire en sorte que tous les liens dans l'élément avec `id="contents"` demande à l'utilisateur s'il veut vraiment partir. Et s'il ne veut pas ne suivez pas le lien.

Like this:
Commce ceci:

[iframe height=100 border=1 src="solution"]

Details:
Détails:

- HTML inside the element may be loaded or regenerated dynamically at any time, so we can't find all links and put handlers on them. Use event delegation.
- The content may have nested tags. Inside links too, like `<a href=".."><i>...</i></a>`.
- Le HTML à l'intérieur de l'élément peut être chargé ou regénéré dynamiquement à n'importe quel moment, donc nous ne pouvons pas trouver tous les liens et mettre des gestionnaires dessus. Utilisez la délégation d'évènement.
- Le contenu peut avoir des éléments imbriqués. À l'intérieur des liens aussi, comme ceci `<a href=".."><i>...</i></a>`.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
The solution is to assign the handler to the container and track clicks. If a click is on the `<a>` link, then change `src` of `#largeImg` to the `href` of the thumbnail.
La solution est d'assigner le gestionnaire au conteneur et suivre les clics. Si un clic est sur le lien `<a>`, alors changer `src` de `#largeImg` pour le `href` de la miniature.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<p><img id="largeImg" src="https://en.js.cx/gallery/img1-lg.jpg" alt="Large image"></p>

<ul id="thumbs">
<!-- the browser shows a small built-in tooltip on hover with the text from "title" attribute -->
<!-- le navigateur affiche une petite infobulle lors du survol avec le texte de l'attribut "title" -->
<li>
<a href="https://en.js.cx/gallery/img2-lg.jpg" title="Image 2"><img src="https://en.js.cx/gallery/img2-thumb.jpg"></a>
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<p><img id="largeImg" src="https://en.js.cx/gallery/img1-lg.jpg" alt="Large image"></p>

<ul id="thumbs">
<!-- the browser shows a small built-in tooltip on hover with the text from "title" attribute -->
<!-- le navigateur affiche une petite infobulle lors du survol avec le texte de l'attribut "title" -->
<li>
<a href="https://en.js.cx/gallery/img2-lg.jpg" title="Image 2"><img src="https://en.js.cx/gallery/img2-thumb.jpg"></a>
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ importance: 5

---

# Image gallery
# Galerie d'images

Create an image gallery where the main image changes by the click on a thumbnail.
Créer une galerie d'images dans laquelle l'image principale change lors d'un clic sur une miniature.

Like this:
Comme ceci:

[iframe src="solution" height=600]

P.S. Use event delegation.
P.S. Utilisez la délégation d'évènement.
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy