Browser default actions by EmptyWork · Pull Request #245 · javascript-tutorial/id.javascript.info · GitHub
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
Expand Up @@ -2,9 +2,9 @@ importance: 3

---

# Why "return false" doesn't work?
# Mengapa "return false" tidak berfungsi?

Why in the code below `return false` doesn't work at all?
Kenapa pada kode dibawah `return false` tidak berfungsi sama sekali?

```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()">browser akan membuka w3.org</a>
```

The browser follows the URL on click, but we don't want it.
_browser_ akan mengikuti URL yang di klik, tapi kita tidak mau hal itu terjadi.

How to fix?
Bagaimana cara memperbaikinya?
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
That's a great use of the event delegation pattern.
Itu merupakan salah satu cara yang bagus dalam pemanfaatan pola delegasi peristiwa.

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).
Pada kehidupan nyata kita bisa mengirim permintaan "logging" ke server yang menyimpan informasi tentang dari mana pengujung meninggalkan website daripada menanyakannya secara langsung. Atau kita bisa memuat konten dan menunjukannya tepat pada halaman (jika diizinkan).

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.
Yang kita butuhkan hanyalah menangkap `contents.onclick` dan menggunakan `confim` untuk menanyakan pengguna. Sebuah ide bagus adalah dengan menggunakan `link.getAttribute('href')` dari pada menggunakan `link.href` untuk URL. Lihat solusinya untuk rincian.
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
<body>

<fieldset id="contents">
<legend>#contents</legend>
<legend>#kontent</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?
Bagaimana jika membaca di <a href="https://wikipedia.org">Wikipedia</a> atau kunjungi <a href="https://w3.org"><i>W3.org</i></a> dan belajar tentang standar terbaru?
</p>
</fieldset>

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
<body>

<fieldset id="contents">
<legend>#contents</legend>
<legend>#kontent</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?
Bagaimana jika membaca di <a href="https://wikipedia.org">Wikipedia</a> atau kunjungi <a href="https://w3.org"><i>W3.org</i></a> dan belajar tentang standar terbaru?
</p>
</fieldset>

Expand Down
Loading