Skip to content
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.
Quando o navegador lê o atributo `on*`, como `onclick`, ele cria o manipulador a partir do seu conteúdo.

For `onclick="handler()"` the function will be:
Para `onclick="handler()"` a função será:

```js
function(event) {
handler() // the content of onclick
handler() // o conteúdo de onclick
}
```

Now we can see that the value returned by `handler()` is not used and does not affect the result.
Agora podemos ver que o valor retornado por `handler()` não é usado e não afeta o resultado.

The fix is simple:
A correção é simples:

```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:
Também podemos usar `event.preventDefault()`, assim:

```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?
# Por que "return false" não funciona?

Why in the code below `return false` doesn't work at all?
Por que no código abaixo `return false` não funciona de forma alguma?

```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()">o navegador vai para w3.org</a>
```

The browser follows the URL on click, but we don't want it.
O navegador segue a URL ao clicar, mas não queremos isso.

How to fix?
Como corrigir?
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
That's a great use of the event delegation pattern.
Esse é um ótimo uso do padrão de delegação de eventos.

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).
Na vida real, em vez de perguntar, podemos enviar uma requisição de "log" ao servidor que salva a informação sobre por onde o visitante saiu. Ou podemos carregar o conteúdo e mostrá-lo diretamente na página (se permitido).

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.
Tudo que precisamos é capturar `contents.onclick` e usar `confirm` para perguntar ao usuário. Uma boa ideia seria usar `link.getAttribute('href')` em vez de `link.href` para a URL. Veja a solução para os detalhes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
<fieldset id="contents">
<legend>#contents</legend>
<p>
How about to read <a href="https://wikipedia.org">Wikipedia</a> or visit <a href="https://w3.org"><i>W3.org</i></a> and learn about modern standards?
Que tal ler a <a href="https://wikipedia.org">Wikipedia</a> ou visitar <a href="https://w3.org"><i>W3.org</i></a> e aprender sobre os padrões modernos?
</p>
</fieldset>

<script>
contents.onclick = function(event) {

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

Expand All @@ -35,6 +35,6 @@
}
};
</script>

</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<fieldset id="contents">
<legend>#contents</legend>
<p>
How about to read <a href="https://wikipedia.org">Wikipedia</a> or visit <a href="https://w3.org"><i>W3.org</i></a> and learn about modern standards?
Que tal ler a <a href="https://wikipedia.org">Wikipedia</a> ou visitar <a href="https://w3.org"><i>W3.org</i></a> e aprender sobre os padrões modernos?
</p>
</fieldset>

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
# Capture links no elemento

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.
Faça com que todos os links dentro do elemento com `id="contents"` perguntem ao usuário se ele realmente quer sair. E, se ele não quiser, não navegue.

Like this:
Assim:

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

Details:
Detalhes:

- 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>`.
- O HTML dentro do elemento pode ser carregado ou regenerado dinamicamente em qualquer momento, então não podemos encontrar todos os links e colocar manipuladores neles. Use delegação de eventos.
- O conteúdo pode ter tags aninhadas. Dentro de links também, como `<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.
A solução é atribuir o manipulador ao container e rastrear os cliques. Se um clique for no link `<a>`, então mude o `src` de `#largeImg` para o `href` da miniatura.
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@
<html>

<head>
<title>Gallery</title>
<title>Galeria</title>
<link rel="stylesheet" href="gallery.css">
<meta charset="utf-8">
</head>

<body>

<p><img id="largeImg" src="https://en.js.cx/gallery/img1-lg.jpg" alt="Large image"></p>
<p><img id="largeImg" src="https://en.js.cx/gallery/img1-lg.jpg" alt="Imagem grande"></p>

<ul id="thumbs">
<!-- the browser shows a small built-in tooltip on hover with the text from "title" attribute -->
<!-- o navegador mostra uma pequena tooltip nativa ao passar o mouse, com o texto do atributo "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>
<a href="https://en.js.cx/gallery/img2-lg.jpg" title="Imagem 2"><img src="https://en.js.cx/gallery/img2-thumb.jpg"></a>
</li>
<li>
<a href="https://en.js.cx/gallery/img3-lg.jpg" title="Image 3"><img src="https://en.js.cx/gallery/img3-thumb.jpg"></a>
<a href="https://en.js.cx/gallery/img3-lg.jpg" title="Imagem 3"><img src="https://en.js.cx/gallery/img3-thumb.jpg"></a>
</li>
<li>
<a href="https://en.js.cx/gallery/img4-lg.jpg" title="Image 4"><img src="https://en.js.cx/gallery/img4-thumb.jpg"></a>
<a href="https://en.js.cx/gallery/img4-lg.jpg" title="Imagem 4"><img src="https://en.js.cx/gallery/img4-thumb.jpg"></a>
</li>
<li>
<a href="https://en.js.cx/gallery/img5-lg.jpg" title="Image 5"><img src="https://en.js.cx/gallery/img5-thumb.jpg"></a>
<a href="https://en.js.cx/gallery/img5-lg.jpg" title="Imagem 5"><img src="https://en.js.cx/gallery/img5-thumb.jpg"></a>
</li>
<li>
<a href="https://en.js.cx/gallery/img6-lg.jpg" title="Image 6"><img src="https://en.js.cx/gallery/img6-thumb.jpg"></a>
<a href="https://en.js.cx/gallery/img6-lg.jpg" title="Imagem 6"><img src="https://en.js.cx/gallery/img6-thumb.jpg"></a>
</li>
</ul>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@
<html>

<head>
<title>Gallery</title>
<title>Galeria</title>
<link rel="stylesheet" href="gallery.css">
<meta charset="utf-8">
</head>

<body>

<p><img id="largeImg" src="https://en.js.cx/gallery/img1-lg.jpg" alt="Large image"></p>
<p><img id="largeImg" src="https://en.js.cx/gallery/img1-lg.jpg" alt="Imagem grande"></p>

<ul id="thumbs">
<!-- the browser shows a small built-in tooltip on hover with the text from "title" attribute -->
<!-- o navegador mostra uma pequena tooltip nativa ao passar o mouse, com o texto do atributo "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>
<a href="https://en.js.cx/gallery/img2-lg.jpg" title="Imagem 2"><img src="https://en.js.cx/gallery/img2-thumb.jpg"></a>
</li>
<li>
<a href="https://en.js.cx/gallery/img3-lg.jpg" title="Image 3"><img src="https://en.js.cx/gallery/img3-thumb.jpg"></a>
<a href="https://en.js.cx/gallery/img3-lg.jpg" title="Imagem 3"><img src="https://en.js.cx/gallery/img3-thumb.jpg"></a>
</li>
<li>
<a href="https://en.js.cx/gallery/img4-lg.jpg" title="Image 4"><img src="https://en.js.cx/gallery/img4-thumb.jpg"></a>
<a href="https://en.js.cx/gallery/img4-lg.jpg" title="Imagem 4"><img src="https://en.js.cx/gallery/img4-thumb.jpg"></a>
</li>
<li>
<a href="https://en.js.cx/gallery/img5-lg.jpg" title="Image 5"><img src="https://en.js.cx/gallery/img5-thumb.jpg"></a>
<a href="https://en.js.cx/gallery/img5-lg.jpg" title="Imagem 5"><img src="https://en.js.cx/gallery/img5-thumb.jpg"></a>
</li>
<li>
<a href="https://en.js.cx/gallery/img6-lg.jpg" title="Image 6"><img src="https://en.js.cx/gallery/img6-thumb.jpg"></a>
<a href="https://en.js.cx/gallery/img6-lg.jpg" title="Imagem 6"><img src="https://en.js.cx/gallery/img6-thumb.jpg"></a>
</li>
</ul>

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

---

# Image gallery
# Galeria de imagens

Create an image gallery where the main image changes by the click on a thumbnail.
Crie uma galeria de imagens onde a imagem principal muda ao clicar em uma miniatura.

Like this:
Assim:

[iframe src="solution" height=600]

P.S. Use event delegation.
P.S. Use delegação de eventos.
Loading