You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/01-hello-world/2-hello-alert-ext/task.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,8 +2,8 @@ importance: 5
2
2
3
3
---
4
4
5
-
# Show an alert with an external script
5
+
# Zeig einen Alarm mit einem externen Skript
6
6
7
-
Take the solution of the previous task <info:task/hello-alert>. Modify it by extracting the script content into an external file `alert.js`, residing in the same folder.
7
+
Nimm die Lösung der vorherigen Aufgabe <info:task/hello-alert>. Modifiziere sie durch auslagern des Skript-Contents in eine externe Datei, innerhalb des gleichen Ordners.
8
8
9
-
Open the page, ensure that the alert works.
9
+
Öffne die Seite, stelle sicher, dass der Alarm funktioniert.
This part of the tutorial is about core JavaScript, the language itself.
3
+
Dieser Teil des Tutorials behandelt den Kern von JavaScript, die Sprache selbst.
4
4
5
-
But we need a working environment to run our scripts and, since this book is online, the browser is a good choice. We'll keep the amount of browser-specific commands (like`alert`) to a minimum so that you don't spend time on them if you plan to concentrate on another environment (like Node.js). We'll focus on JavaScript in the browser in the [next part](/ui)of the tutorial.
5
+
Aber wir brauchen eine Arbeitsumgebung um unsere Skripte auszuführen und, da dies ein Online-Buch ist, ist der Browser eine gute Wahl. Wir werden die Anzahl browser-spezifischer Befehle (wie`alert`) auf ein Minimum beschränken, so dass du damit keine Zeit verschwendest, solltest du planen dich auf eine andere Umgebung (wie Node.js) zu konzentrieren. Wir fokusieren uns auf JavaScript im Browser im [nächsten Teil](/ui)des Tutorials.
6
6
7
-
So first, let's see how we attach a script to a webpage. For server-side environments (like Node.js), you can execute the script with a command like `"node my.js"`.
7
+
Als erstes, lass uns sehen wie wir ein Skript einer Webseite hinzufügen. Für eine serverseitige Umgebung (wie Node.js), kannst du das Skript mit einem Befehl wie `"node mein.js"` ausführen.
8
8
9
9
10
-
## The "script" tag
10
+
## Der "script"-Tag
11
11
12
-
JavaScript programs can be inserted into any part of an HTML document with the help of the `<script>` tag.
12
+
JavaScript-Programme können an jeder beliebigen Stelle eines HTML-Dokuments mit Hilfe des `<script>`-Tag eingefügt werden.
13
13
14
-
For instance:
14
+
Beispielsweise:
15
15
16
16
```html run height=100
17
17
<!DOCTYPE HTML>
18
18
<html>
19
19
20
20
<body>
21
21
22
-
<p>Before the script...</p>
22
+
<p>Vor dem Skript...</p>
23
23
24
24
*!*
25
25
<script>
26
-
alert( 'Hello, world!' );
26
+
alert( 'Hallo, Welt!' );
27
27
</script>
28
28
*/!*
29
29
30
-
<p>...After the script.</p>
30
+
<p>...Nach dem Skript</p>
31
31
32
32
</body>
33
33
34
34
</html>
35
35
```
36
36
37
37
```online
38
-
You can run the example by clicking the "Play" button in the right-top corner of the box above.
38
+
Du kannst das Beispiel durch klicken des "Play"-Buttons, in der rechten oberen Ecke der darüberliegenden Box, ausführen.
39
39
```
40
40
41
-
The`<script>` tag contains JavaScript code which is automatically executed when the browser processes the tag.
41
+
Der`<script>`-Tag beinhaltet JavaScript-Code, welcher automatisch ausgeführt wird, wenn der Browser den Tag verarbeitet.
42
42
43
43
44
-
## Modern markup
44
+
## Modernes Markup
45
45
46
-
The`<script>` tag has a few attributes that are rarely used nowadays but can still be found in old code:
46
+
Der`<script>`-Tag hat einige Attribute die heutzutage nur selten Verwendung finden, aber noch immer in alten Code vorhanden sind:
: The old HTML standard, HTML4, required a script to have a `type`. Usually it was`type="text/javascript"`. It's not required anymore. Also, the modern HTML standard totally changed the meaning of this attribute. Now, it can be used for JavaScript modules. But that's an advanced topic; we'll talk about modules in another part of the tutorial.
: Der alte HTML-Standard, HTML4, setzte bei einem Skript den `Typ` voraus. Üblicherweise war es`type="text/javascript"`. Dies wird nicht länger benötigt. Zudem, der moderne HTML-Standard hat die Bedeutung des Attributs völlig verändert. Jetzt kann es für JavaScript-Module verwendet werden. Aber dies ist ein weiterführendes Thema; wir werden über Module in einem anderen Teil des Tutorials sprechen.
: This attribute was meant to show the language of the script. This attribute no longer makes sense because JavaScript is the default language. There is no need to use it.
: Dieses Attribute war gedacht, um die Sprache des Skripts anzuzeigen. Dieses Attribute macht nicht länger Sinn, weil JavaScript die Standard-Sprache ist. Es gibt keinen Grund es zu nutzen.
53
53
54
-
Comments before and after scripts.
55
-
: In really ancient books and guides, you may find comments inside`<script>` tags, like this:
54
+
Kommentare vor und nach Skripten.
55
+
: In wirklich alten Büchern und Anleitungen findest du vielleicht, innerhalb von`<script>`-Tags, Kommentare wie diese:
56
56
57
57
```html no-beautify
58
58
<script type="text/javascript"><!--
59
59
...
60
60
//--></script>
61
61
```
62
62
63
-
This trick isn't used in modern JavaScript. These comments hide JavaScript code from old browsers that didn't know how to process the `<script>` tag. Since browsers released in the last 15 years don't have this issue, this kind of comment can help you identify really old code.
63
+
Dieser Trick wird im modernen JavaScript nicht verwendet. Diese Kommentare verstecken JavaScript-Code für alte Browser, die nicht wissen wie der `<script>`-Tag verarbeitet wird. Da Browser-Veröffentlichungen der letzten 15 Jahre dieses Problem nicht haben, kann dir diese Art von Kommentar helfen wirklich alten Code zu identifizieren.
64
64
65
65
66
-
## External scripts
66
+
## Externe Skripte
67
67
68
-
If we have a lot of JavaScript code, we can put it into a separate file.
68
+
Wenn wir viel JavaScript-Code haben, können wir diesen in eine separate Datei legen.
69
69
70
-
Script files are attached to HTML with the`src` attribute:
70
+
Skript-Dateien werden zu HTML mit dem`src`-Attribute hinzugefügt:
71
71
72
72
```html
73
-
<scriptsrc="/path/to/script.js"></script>
73
+
<scriptsrc="/pfad/zum/script.js"></script>
74
74
```
75
75
76
-
Here, `/path/to/script.js`is an absolute path to the script from the site root. One can also provide a relative path from the current page. For instance, `src="script.js"`would mean a file `"script.js"`in the current folder.
76
+
Hier ist `/pfad/zum/script.js`ein absoluter Pfad zu dem Skript, aus dem Wurzelverzeichnis der Seite. Auch ein relativer Pfad der aktuellen Seite kann angeben werden. Beispielsweise, `src="script.js"`würde eine Datei `"script.js"`im aktuellen Verzeichnis bedeuten.
77
77
78
-
We can give a full URL as well. For instance:
78
+
Außerdem können wir auch eine komplette URL angeben. Beispielsweise:
Um mehrere Skripte einzufügen, nutze Mehrfach-Tags:
85
85
86
86
```html
87
87
<scriptsrc="/js/script1.js"></script>
@@ -90,43 +90,43 @@ To attach several scripts, use multiple tags:
90
90
```
91
91
92
92
```smart
93
-
As a rule, only the simplest scripts are put into HTML. More complex ones reside in separate files.
93
+
Normalerweise werden nur die einfachsten Skripte ins HTML eingefügt. Komplexere liegen in separaten Dateien.
94
94
95
-
The benefit of a separate file is that the browser will download it and store it in its [cache](https://en.wikipedia.org/wiki/Web_cache).
95
+
Der Vorteil einer separaten Datei ist, dass der Browser sie heruntlädt und in seinem [Puffer-Speicher](https://de.wikipedia.org/wiki/Browser-Cache) ablegt.
96
96
97
-
Other pages that reference the same script will take it from the cache instead of downloading it, so the file is actually downloaded only once.
97
+
Andere Seiten die zum selben Skript referenzieren werden es aus dem Speicher nehmen anstatt es herunterzuladen, die Datei wird so tatsächlich nur einmal geladen.
98
98
99
-
That reduces traffic and makes pages faster.
99
+
Das reduziert Datenverkehr und macht Seiten schneller.
100
100
```
101
101
102
-
````warn header="If`src`is set, the script content is ignored."
103
-
A single`<script>` tag can't have both the`src` attribute and code inside.
102
+
````warn header="Wenn`src`gesetzt ist, wird der Skript-Content ignoriert."
103
+
Ein einzelner`<script>`-Tag kann nicht beides haben, das`src`-Attribute und Code innerhalb.
104
104
105
-
This won't work:
105
+
Dies funktioniert nicht:
106
106
107
107
```html
108
-
<script*!*src*/!*="file.js">
109
-
alert(1); //the content is ignored, because src is set
108
+
<script*!*src*/!*="datei.js">
109
+
alert(1); //der Inhalt wird ignoriert, weil src ist gesetzt.
110
110
</script>
111
111
```
112
112
113
-
We must choose either an external`<script src="…">`or a regular`<script>`with code.
113
+
Wir müssen wählen, entweder ein externes`<script src="…">`oder ein reguläres`<script>`mit Code.
114
114
115
-
The example above can be split into two scripts to work:
115
+
Um zu funktionieren kann das obige Beispiel in zwei Skripte unterteilt werden:
116
116
117
117
```html
118
-
<scriptsrc="file.js"></script>
118
+
<scriptsrc="datei.js"></script>
119
119
<script>
120
120
alert(1);
121
121
</script>
122
122
```
123
123
````
124
124
125
-
## Summary
125
+
## Zusammenfassung
126
126
127
-
- We can use a `<script>` tag to add JavaScript code to a page.
128
-
- The `type` and `language` attributes are not required.
129
-
- A script in an external file can be inserted with `<script src="path/to/script.js"></script>`.
127
+
- Wir können einen `<script>`-Tag nutzen, um JavaScript-Code einer Seite hinzuzufügen.
128
+
- Die `type`- und `language`-Attribute sind nicht erforderlich.
129
+
- Ein Skript in einer externen Datei kann mit `<script src="pfad/zum/skript.js"></script>` eingefügt werden.
130
130
131
131
132
-
There is much more to learn about browser scripts and their interaction with the webpage. But let's keep in mind that this part of the tutorial is devoted to the JavaScript language, so we shouldn't distract ourselves with browser-specific implementations of it. We'll be using the browser as a way to run JavaScript, which is very convenient for online reading, but only one of many.
132
+
Es gibt noch viel mehr über Browser-Skripte und ihre Interaktion mit Webseiten zu lernen. Aber vergessen wir nicht, dass dieser Teil des Tutorials JavaScript selbst gewidmet ist, wir sollten uns nicht von browser-spezifischen Implementierungen ablenken lassen. Wir werden den Browser als Methode zum Ausführen von JavaScript verwenden, was für das Online-Lesen sehr praktisch ist, aber nur eine von vielen.
0 commit comments