JavaScript - onload Event: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „Um auf eine Seite zu Warten bevor man JavaScript ausführt (normalerweise auf die Bilder) window.onload = function () { alert("It's loaded!") } Vorsicht: Man…“)
 
 
(2 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
Um auf eine Seite zu Warten bevor man JavaScript ausführt (normalerweise auf die Bilder)  
+
Um auf eine Seite zu Warten bevor man JavaScript ausführt (oft auf die Bilder)  
 
  window.onload = function () { alert("It's loaded!") }
 
  window.onload = function () { alert("It's loaded!") }
 +
 
Vorsicht: Manche ältere Browser feuern das Event nicht wenn man in der Browserhistory skrollt
 
Vorsicht: Manche ältere Browser feuern das Event nicht wenn man in der Browserhistory skrollt
 +
  
 
=== Hacks ===
 
=== Hacks ===
 
Usually you can use window.onload, but you may notice that recent browsers don't fire window.onload when you use the back/forward history buttons.
 
Usually you can use window.onload, but you may notice that recent browsers don't fire window.onload when you use the back/forward history buttons.
  
Some people suggest weird contortions to work around this problem, but really if you just make a window.onunload handler (even one that doesn't do anything), this caching behavior will be disabled in all browsers. The MDC documents this "feature" pretty well, but for some reason there are still people using setInterval and other weird hacks.
+
Some people suggest weird contortions to work around this problem, but really if you just make a '''window.onunload handler''' (even one that doesn't do anything), this caching behavior will be disabled in all browsers. The MDC documents this "feature" pretty well, but for some reason there are still people using setInterval and other weird hacks.
  
 
Some versions of Opera have a bug that can be worked around by adding the following somewhere in your page:
 
Some versions of Opera have a bug that can be worked around by adding the following somewhere in your page:

Aktuelle Version vom 5. Dezember 2019, 16:58 Uhr

Um auf eine Seite zu Warten bevor man JavaScript ausführt (oft auf die Bilder)

window.onload = function () { alert("It's loaded!") }

Vorsicht: Manche ältere Browser feuern das Event nicht wenn man in der Browserhistory skrollt


Hacks[Bearbeiten]

Usually you can use window.onload, but you may notice that recent browsers don't fire window.onload when you use the back/forward history buttons.

Some people suggest weird contortions to work around this problem, but really if you just make a window.onunload handler (even one that doesn't do anything), this caching behavior will be disabled in all browsers. The MDC documents this "feature" pretty well, but for some reason there are still people using setInterval and other weird hacks.

Some versions of Opera have a bug that can be worked around by adding the following somewhere in your page:

<script>history.navigationMode = 'compatible';</script>

If you're just trying to get a javascript function called once per-view (and not necessarily after the DOM is finished loading), you can do something like this:

<img src="javascript:location.href='javascript:yourFunction();';">

For example, I use this trick to preload a very large file into the cache on a loading screen:

img src="bigfile"
onload="this.location.href='javascript:location.href=\'javascript:doredir();\';';doredir();">