Skip to content

Commit 6de81a0

Browse files
authored
Merge pull request #356 from cys27/master
Scripts: async, defer, Bezier curve
2 parents 034e927 + 5d8fe75 commit 6de81a0

File tree

2 files changed

+158
-158
lines changed

2 files changed

+158
-158
lines changed
Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,152 +1,152 @@
11

2-
# Scripts: async, defer
2+
# Komut Dosyaları (Scripts): async, defer
33

4-
In modern websites, scripts are often "heavier" than HTML: their download size is larger, and processing time is also longer.
4+
Modern websitelerinde, genellikle script'ler HTML'den daha baskındır: script'lerin dosya/indirme boyutları büyüktür ve işlenme süreleri uzundur.
55

6-
When the browser loads HTML and comes across a `<script>...</script>` tag, it can't continue building DOM. It must execute the script right now. The same happens for external scripts `<script src="..."></script>`: the browser must wait until the script downloads, execute it, and only after process the rest of the page.
6+
Tarayıcı, HTML'i yüklerken `<script>...</script>` etiketiyle karşılaştığında, DOM'u oluşturmaya devam edemez. Böyle bir durumda script'i çalıştırmak zorundadır. Benzer durum `<script src="..."></script>` şeklinde dışarıdan aktarılan script'ler içinde geçerlidir: Tarayıcı script indirilene kadar bekleyecek, sonrasında onu çalıştıracak ve en sonunda sayfanın geri kalananı işleyecektir.
77

8-
That leads to two important issues:
8+
Bu durum iki önemli soruna yol açar:
99

10-
1. Scripts can't see DOM elements below them, so can't add handlers etc.
11-
2. If there's a bulky script at the top of the page, it "blocks the page". Users can't see the page content till it downloads and runs:
10+
1. Script'ler, onların altındaki DOM öğelerini (element) göremeyebilir, yani işleyici fonksiyonlar (handlers) vb. ekleyemezsiniz.
11+
2. Sayfanın üst kısmında büyük bir script varsa, bu "sayfanın yüklenmesini engeller". Kullanıcılar, script indirilip, çalıştırılana kadar sayfa içeriğini göremez.
1212

1313
```html run height=100
14-
<p>...content before script...</p>
14+
<p>...script'ten önceki içerik...</p>
1515

1616
<script src="https://javascript.info/article/script-async-defer/long.js?speed=1"></script>
1717

18-
<!-- This isn't visible until the script loads -->
19-
<p>...content after script...</p>
18+
<!-- Bu, script yüklenene kadar gözükmeyecek -->
19+
<p>...script'ten sonraki içerik...</p>
2020
```
2121

22-
There are some workarounds to that. For instance, we can put a script at the bottom of the page. Then it can see elements above it, and it doesn't block the page content from showing:
22+
Bunun içi bazı geçici çözümler vardır. Örneğin, script'i sayfanın alt kısmına yerleştirebiliriz. Bu sayede script, kendinden önce bulunan öğeleri görebilir ve sayfa içeriğinin görüntülenmesini engellemez:
2323

2424
```html
2525
<body>
26-
...all content is above the script...
26+
...tüm içerik script'in üzerindedir...
2727

2828
<script src="https://javascript.info/article/script-async-defer/long.js?speed=1"></script>
2929
</body>
3030
```
3131

32-
But this solution is far from perfect. For example, the browser notices the script (and can start downloading it) only after it downloaded the full HTML document. For long HTML documents, that may be a noticeable delay.
32+
Fakat bu çözüm mükemmel olmaktan uzaktır. Örneğin, tarayıcı, script'i HTML belgesinin tamamını indirdikten sonra farkeder (ve onu indirmeye başlayabilir). Uzun HTML belgelesi için, bu fark edilebilir bir gecikme olabilir.
3333

34-
Such things are invisible for people using very fast connections, but many people in the world still have slower internet speeds and use far-from-perfect mobile internet.
34+
Bu tür durumlar çok hızlı bir internet bağlantısına sahip olanlar için önemsizdir, fakat dünyada birçok insan hala yavaş bir internet hızına sahip ve mükemmel olmaktan uzak olan mobil interneti kullanıyor.
3535

36-
Luckily, there are two `<script>` attributes that solve the problem for us: `defer` and `async`.
36+
Neyse ki, bizim için bu sorunu çözen iki tane `<script>` niteliği (attribute) vardır: `defer` ve `async`.
3737

3838
## defer
3939

40-
The `defer` attribute tells the browser that it should go on working with the page, and load the script "in background", then run the script when it loads.
40+
`defer` niteliği, tarayıcıya sayfayı yüklemeye devam etmesini, ve script'in "arkaplanda" yüklemesini, sonrasında sayfa yüklendikten sonra script'in çalıştırılmasını söyler.
4141

42-
Here's the same example as above, but with `defer`:
42+
Yukarıdaki ile aynı örnek, fakat burada `defer` niteliği mevcut:
4343

4444
```html run height=100
45-
<p>...content before script...</p>
45+
<p>...script'lerden önceki içerik...</p>
4646

4747
<script defer src="https://javascript.info/article/script-async-defer/long.js?speed=1"></script>
4848

49-
<!-- visible immediately -->
50-
<p>...content after script...</p>
49+
<!-- hemen görülebilir -->
50+
<p>...script'lerden sonraki içerik...</p>
5151
```
5252

53-
- Scripts with `defer` never block the page.
54-
- Scripts with `defer` always execute when the DOM is ready, but before `DOMContentLoaded` event.
53+
- `defer` kullanılan script, sayfayı engellemez.
54+
- `defer` kullanılan script, her zaman DOM hazır olduğunda, `DOMContentLoaded` olayından (event) önce çalıştırılır.
5555

56-
The following example demonstrates that:
56+
Aşağıdaki örnek bunu göstermektedir:
5757

5858
```html run height=100
59-
<p>...content before scripts...</p>
59+
<p>...script'lerden önceki içerik...</p>
6060

6161
<script>
62-
document.addEventListener('DOMContentLoaded', () => alert("DOM ready after defer!")); // (2)
62+
document.addEventListener('DOMContentLoaded', () => alert("DOM ertelemeden (defer) sonra hazır!")); // (2)
6363
</script>
6464

6565
<script defer src="https://javascript.info/article/script-async-defer/long.js?speed=1"></script>
6666

67-
<p>...content after scripts...</p>
67+
<p>...script'lerden sonraki içerik...</p>
6868
```
6969

70-
1. The page content shows up immediately.
71-
2. `DOMContentLoaded` waits for the deferred script. It only triggers when the script `(2)` is downloaded is executed.
70+
1. Sayfa içeriği hemen görünür.
71+
2. `DOMContentLoaded`, ertelenmiş (deferred) script'i bekler. Sadece script `(2)` indirilip, çalıştırıldığında tetiklenir.
7272

73-
Deferred scripts keep their relative order, just like regular scripts.
73+
Ertelenmiş script'ler (deferred scripts), tıpkı normal script'ler gibi göreli sıralarını korurlar.
7474

75-
So, if we have a long script first, and then a smaller one, then the latter one waits.
75+
Yani, ilk olarak büyük bir script'e ve sonrasında küçük bir tanesine sahipsek, sonuncusu bekler.
7676

7777
```html
7878
<script defer src="https://javascript.info/article/script-async-defer/long.js"></script>
7979
<script defer src="https://javascript.info/article/script-async-defer/small.js"></script>
8080
```
8181

82-
```smart header="The small script downloads first, runs second"
83-
Browsers scan the page for scripts and download them in parallel, to improve performance. So in the example above both scripts download in parallel. The `small.js` probably makes it first.
82+
```smart header="Küçük komut dosyası önce indirilir, sonra çalıştırılır."
83+
Tarayıcılar, performansı artırmak için sayfadaki komut dosyalarını tarar ve paralel/eş zamanlı olarak indirmeye başlar. Yani yukarıdaki örnekte her iki komut dosyasıda eş zamanlı olarak indirilir. Muhtemelen `small.js` ilk önce indirilecektir.
8484
85-
But the specification requires scripts to execute in the document order, so it waits for `long.js` to execute.
85+
Ancak komut dosyalarının sayfadaki sıraya göre çalıştırılması gerekir, bu nedenle `long.js` çalıştırılmasını bekler.
8686
```
8787

88-
```smart header="The `defer` attribute is only for external scripts"
89-
The `defer` attribute is ignored if the `<script>` tag has no `src`.
88+
```smart header="`defer` niteliği yalnızca dışarıdan aktarılan komut dosyaları içindir"
89+
Eğer `<script>` etiketinde `src` yoksa `defer` niteliği yok sayılır.
9090
```
9191
9292
9393
## async
9494
95-
The `async` attribute means that a script is completely independent:
95+
`async` niteliği, bir script'in tamamiyle bağımsız olduğu anlamına gelir:
9696
97-
- The page doesn't wait for async scripts, the contents is processed and displayed.
98-
- `DOMContentLoaded` and async scripts don't wait each other:
99-
- `DOMContentLoaded` may happen both before an async script (if an async script finishes loading after the page is complete)
100-
- ...or after an async script (if an async script is short or was in HTTP-cache)
101-
- Other scripts don't wait for `async` scripts, and `async` scripts don't wait for them.
97+
- Sayfa asenkron script'leri (async scripts) beklemez, içerik işlenir ve görüntülenir.
98+
- `DOMContentLoaded` ve asenkron script'ler (async scripts) birbirlerini beklemezler:
99+
- `DOMContentLoaded` ya bir asenkron script'ten (async script) önce gerçekleşebilir (bir asenkron script sayfa tamamlandıktan sonra yüklemeyi bitirirse)
100+
- ...ya da bir asenkron script'ten sonra (bir asenkron script küçük ya da HTTP önbelleğinde mevcut ise)
101+
- Diğer script'ler, `async` script'leri için, `async` script'leri de onlar için beklemez.
102102
103103
104-
So, if we have several `async` scripts, they may execute in any order. Whatever loads first -- runs first:
104+
Dolasıyla, birden fazla `async` script'imiz varsa, onlar herhangi bir sırada çalıştırılır. İlk önce hangisi yüklenirse o çalıştırılır:
105105
106106
```html run height=100
107-
<p>...content before scripts...</p>
107+
<p>...script'lerden önceki içerik...</p>
108108
109109
<script>
110-
document.addEventListener('DOMContentLoaded', () => alert("DOM ready!"));
110+
document.addEventListener('DOMContentLoaded', () => alert("DOM hazır!"));
111111
</script>
112112
113113
<script async src="https://javascript.info/article/script-async-defer/long.js"></script>
114114
<script async src="https://javascript.info/article/script-async-defer/small.js"></script>
115115
116-
<p>...content after scripts...</p>
116+
<p>...script'lerden sonraki içerik...</p>
117117
```
118118

119-
1. The page content shows up immediately: `async` doesn't block it.
120-
2. `DOMContentLoaded` may happen both before and after `async`, no guarantees here.
121-
3. Async scripts don't wait for each other. A smaller script `small.js` goes second, but probably loads before `long.js`, so runs first. That's called a "load-first" order.
119+
1. Sayfa içeriği hemen görünür: `async` sayfayı engellemez.
120+
2. `DOMContentLoaded`, `async`'den öncede gerçekleşebilir, sonrada gerçekleşebilir. Burada garanti yok.
121+
3. Asenkron script'ler birbirlerini beklemezler. Küçük script `small.js` ikinci sıradadır, fakat muhtemelen `long.js`'den önce yüklenecektir, dolayısıyla önce o çalıştırılacaktır. Buna "ilk sıradakini yükle" denir.
122122

123-
Async scripts are great when we integrate an independent third-party script into the page: counters, ads and so on, as they don't depend on our scripts, and our scripts shouldn't wait for them:
123+
Asenkron script'ler, bağımsız bir üçüncü taraf script'i sayfaya eklediğimizde harikadır: sayaçlar, reklamlar vb. bizim script'lerimize bağlı olmadıkları için komut dosyalarımız onları beklememelidir.
124124

125125
```html
126-
<!-- Google Analytics is usually added like this -->
126+
<!-- Google Analytics genellikle bu şekilde eklenir -->
127127
<script async src="https://google-analytics.com/analytics.js"></script>
128128
```
129129

130130

131-
## Dynamic scripts
131+
## Dinamik Komut Dosyaları (Dynamic Scripts)
132132

133-
We can also add a script dynamically using JavaScript:
133+
Ayrıca, JavaScript kullanarak dinamik olarak bir script ekleyebiliriz:
134134

135135
```js run
136136
let script = document.createElement('script');
137137
script.src = "/article/script-async-defer/long.js";
138138
document.body.append(script); // (*)
139139
```
140140

141-
The script starts loading as soon as it's appended to the document `(*)`.
141+
Script `(*)`, belgeye eklenir eklenmez yüklenmeye başlar.
142142

143-
**Dynamic scripts behave as "async" by default.**
143+
**Dinamik scriptler varsayılan olarak "async" gibi davranır..**
144144

145-
That is:
146-
- They don't wait for anything, nothing waits for them.
147-
- The script that loads first -- runs first ("load-first" order).
145+
Yani:
146+
- Onlar herhangi bir şeyi beklemezler, hiçbir şeyde onları beklemez.
147+
- İlk yüklenen script, önce çalıştırılır ("ilk sıradakini yükle")
148148

149-
We can change the load-first order into the document order (just like regular scripts) by explicitly setting `async` property to `false`:
149+
`async` özelliğini `false` olarak ayarlarsak, yükleme sırasını belge sırası olacak şekilde değiştirebiliriz:
150150

151151
```js run
152152
let script = document.createElement('script');
@@ -159,7 +159,7 @@ script.async = false;
159159
document.body.append(script);
160160
```
161161

162-
For example, here we add two scripts. Without `script.async=false` they would execute in load-first order (the `small.js` probably first). But with that flag the order is "as in the document":
162+
Örneğin, burada iki adet script ekledik. `script.async=false` olmadığından ilk sıradakini yükleye göre çalıştırılacaktı (muhtemelen `small.js` önce çalışacaktı). Fakat bu flag sayesinde sıra "belgedeki sıra gibi" olur.
163163

164164

165165
```js run
@@ -170,29 +170,29 @@ function loadScript(src) {
170170
document.body.append(script);
171171
}
172172

173-
// long.js runs first because of async=false
173+
// long.js, async=false olduğundan dolayı önce çalıştırılır.
174174
loadScript("/article/script-async-defer/long.js");
175175
loadScript("/article/script-async-defer/small.js");
176176
```
177177

178178

179-
## Summary
179+
## Özet
180180

181-
Both `async` and `defer` have one common thing: they don't block page rendering. So the user can read page content and get acquanted with the page immediately.
181+
`async` ve `defer` niteliklerinin ortak bir özelliği vardır: sayfanın yüklenmesini (render) engellemezler. Böylece kullanıcı sayfa içeriğini okuyabilir ve sayfayla hemen etkileşime geçebilir.
182182

183-
But there are also essential differences between them:
183+
Ancak aralarında temel farklılıklar vardır:
184184

185-
| | Order | `DOMContentLoaded` |
185+
| | Sıra | `DOMContentLoaded` |
186186
|---------|---------|---------|
187-
| `async` | *Load-first order*. Their document order doesn't matter -- which loads first | Irrelevant. May load and execute while the document has not yet been fully downloaded. That happens if scripts are small or cached, and the document is long enough. |
188-
| `defer` | *Document order* (as they go in the document). | Execute after the document is loaded and parsed (they wait if needed), right before `DOMContentLoaded`. |
187+
| `async` | *İlk sırayı yükle*. Belgedeki sıraları önemleri değildir -- hangisi önce yüklenirse | Alakasız. Henüz belgenin tamamı indirilmemişken yüklenebilir ve çalıştırılabilir. Bu durum, eğer scriptler küçük veya önbellekte mevcut ise ve belge yeterince uzun ise gerçekleşir.|
188+
| `defer` | *Belge sırası* (belgeye girdikleri gibi). | Belge yüklenip, çözümlendendikten sonra (gerekirse beklerler), `DOMContentLoaded` olayından (event) hemen önce çalıştırılır. |
189189

190-
```warn header="Page without scripts should be usable"
191-
Please note that if you're using `defer`, then the page is visible *before* the script loads.
190+
```warn header="Sayfa, scriptler olmadan kullanılabilir olmalıdır."
191+
`defer` kullanıyorsanız, lütfen sayfanın script yüklenmeden *önce* görüntüleneceğini unutmayın.
192192
193-
So the user may read the page, but some graphical components are probably not ready yet.
193+
Yani kullanıcılar sayfayı okuyabilir, fakat bazı grafiksel bileşenler muhtemelen henüz hazır değildir.
194194
195-
There should be "loading" indication in proper places, not-working buttons disabled, to clearly show the user what's ready and what's not.
195+
Kullanıcıya neyin hazır olup, neyin olmadığını göstermek için uygun yerlere "yükleniyor" ifadesi yerleştirilmeli, çalışmayan düğmeler (button) devre dışı bırakılmalıdır.
196196
```
197197

198-
In practice, `defer` is used for scripts that need the whole DOM and/or their relative execution order is important. And `async` is used for independent scripts, like counters or ads. And their relative execution order does not matter.
198+
Pratikte, `defer` tüm DOM'a ihtiyaç duyan ve/ya da göreli yürütme sırası önemli olan scriptler için kullanılır. Ve `async` sayaçlar, reklamlar gibi bağımsız scriptler için kullanılır. Ve onlarda sıra önemli değildir.

0 commit comments

Comments
 (0)