- HTML Атрибут событий onload
- Определение и использование
- Поддержка браузеров
- Различий между HTML 4.01 и HTML5
- Синтаксис
- Значение атрибута
- Технические подробности
- Больше Примеров
- Пример
- Пример
- Связанные страницы
- Событие onload
- Синтаксис
- Значения
- Значение по умолчанию
- Применяется к тегам
- Пример
- Типы тегов
- HTML onload Attribute
- Applies to
- Examples
- Body Example
- Img Example
- Input Example
- Browser Support
- HTML атрибут-событие onload
- Синтаксис атрибута
- Значения атрибута
- Пример использования атрибута
- How do I call a JavaScript function on page load?
- Using the onload event in the tag
- Syntax
- Example
- Methods to call a JavaScript function on page load.
- Call the JavaScript function on page load by using onload event in the HTML body tag.
- Using the window.onload Event in JavaScript
- Syntax
- Example
- Methods to call a JavaScript function on page load.
- Call the JavaScript function on page load by using window.onload event in the JavaScript.
- Using the DOMContentLoaded Event
- Syntax
- Example
- Methods to call a JavaScript function on page load.
- Call the JavaScript function on page load by using document.addEventListener( ) method.
- Conclusion
HTML Атрибут событий onload
Выполненить JavaScript сразу после загрузки страницы:
Больше примеров «Попробуйте сами» ниже.
Определение и использование
Атрибут onload срабатывает, когда объект будет загружен.
Атрибут onload чаще всего используется в элементе для выполнения скрипта после того, как веб-страница полностью загрузила все содержимое (включая изображения, файлы скриптов, файлы CSS и т.д.). Однако его можно использовать и на других элементах (см. «Поддерживаемые теги HTML» ниже).
Атрибут onload можно использовать для проверки типа браузера посетителя и версии браузера, а также для загрузки соответствующей версии веб-страницы на основе информации.
Атрибут onload также может использоваться для работы с файлами cookie (см. «Дополнительные примеры» ниже).
Поддержка браузеров
Различий между HTML 4.01 и HTML5
Синтаксис
Значение атрибута
Технические подробности
Больше Примеров
Пример
С помощью события onload элемент . Предупреждение «Изображение загружено» сразу после загрузки изображения:
Пример
Использование события onload для работы с файлами cookie (с помощью javascript «advanced»):
function checkCookies() var text = «»;
if (navigator.cookieEnabled == true) text = «Cookies включены.»;
> else text = «Файлы cookie не включены.»;
>
document.getElementById(«demo»).innerHTML = text;
>
Связанные страницы
Событие onload
Событие onload используется как указатель, что веб-страница полностью загружена, включая содержание, изображения, стилевые файлы и внешние скрипты.
Синтаксис
Значения
Значение по умолчанию
Применяется к тегам
Пример
@keyframes _transparent < from < opacity: 0; >to < opacity: 1;>> @-webkit-keyframes _transparent < from < opacity: 0; >to < opacity: 1;>> @-moz-keyframes _transparent < from < opacity: 0; >to < opacity: 1;>> #status function loadPage()
В данном примере после загрузки страницы будет выведено сообщение «Страница загружена».
Не выкладывайте свой код напрямую в комментариях, он отображается некорректно. Воспользуйтесь сервисом cssdeck.com или jsfiddle.net, сохраните код и в комментариях дайте на него ссылку. Так и результат сразу увидят.
Типы тегов
HTML5
Блочные элементы
Строчные элементы
Универсальные элементы
Нестандартные теги
Осуждаемые теги
Видео
Документ
Звук
Изображения
Объекты
Скрипты
Списки
Ссылки
Таблицы
Текст
Форматирование
Формы
Фреймы
HTML onload Attribute
The onload attribute fires when an object has been loaded.
onload is most often used within the element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.). However, it can be used on other elements as well (see «Supported HTML tags» below).
For input elements, the onload attribute is only supported when
The onload attribute can be used to check the visitor’s browser type and browser version, and load the proper version of the web page based on the information.
Applies to
The onload attribute is part of the Event Attributes, and can be used on the following elements:
Examples
Body Example
Execute a JavaScript immediately after a page has been loaded:
Img Example
Using onload on an element. Alert «Image is loaded» immediately after an image has been loaded:
Input Example
Using onload on an element. Alert «Image is loaded» immediately after an image has been loaded:
Browser Support
The onload attribute has the following browser support for each element:
Element | |||||
---|---|---|---|---|---|
body | Yes | Yes | Yes | Yes | Yes |
iframe | Yes | Yes | Yes | Yes | Yes |
img | Yes | Yes | Yes | Yes | Yes |
input type=»image» | Yes | Yes | Yes | Yes | Yes |
link | Yes | Yes | Yes | Yes | Yes |
script | Yes | Yes | Yes | Yes | Yes |
style | Yes | Yes | Yes | Yes | Yes |
HTML атрибут-событие onload
Событие onload возникает, когда объект загружен.
Чаще всего атрибут onload используют с тегом , чтобы выполнить скрипт, когда веб-страница полностью загрузила весь контент (включая изображения, файлы скриптов, файлы CSS и т.д.). Тем не менее, этот атрибут также может использоваться и с другими элементами (см. «Поддерживаемые HTML теги» ниже).
Атрибут onload может использоваться для определения типа и версии браузера посетителя, чтобы затем, основываясь на полученной информации, загрузить соответствующую версию веб-страницы.
Также, атрибут onload может использоваться для работы с кукисами (см. Примеры).
Особенности
Поддерживаемые HTML теги: , , , , , , , ,
Синтаксис атрибута
Значения атрибута
Пример использования атрибута
Запускает скрипт сразу после загрузки страницы:
Использование атрибута onload с элементом . Сразу после загрузки изображения будет выведено сообщение «Изображение загружено»:
function loadImage()
Запускает скрипт для работы с кукисами:
How do I call a JavaScript function on page load?
This tutorial will teach us to call a JavaScript function on page load. In many cases, while programming with HTML and JavaScript, programmers need to call a function, while loading the web page or after the web page load finishes. For example, programmers need to show the welcome message to the user on page load, and the programmer needs to call a JavaScript function after the page load event.
There are various ways to call a function on page load or after page load in JavaScript, and we will look at them one by one in this tutorial.
- Using the onload event in the tag
- Using the window.onload Event
- Using the DOMContentloaded Event
Using the onload event in the tag
The first approach for calling a function on the page load is the use an onload event inside the HTML tag. As you know, the HTML body contains the entire content of the web page, and when all HTML body loads on the web browser, it will call the function from the JavaScript.
Syntax
Follow the below syntax to use onload event in the tag.
Example
In the below example, we have created the JavaScript function named welcomeFunction(). We are calling that function from the tag using the onload event, and it shows the welcome message in the alert box to the users.
Methods to call a JavaScript function on page load.
Call the JavaScript function on page load by using onload event in the HTML body tag.
let message = document.getElementById("message"); function welcomeFunction()