Dynamic HTML: Difference between revisions

Content deleted Content added
Bbnck (talk | contribs)
mNo edit summary
No edit summary
 
(34 intermediate revisions by 27 users not shown)
Line 1:
 
{{Short description|Coding interactive or animated websites}}
{{Manualhow-to|date=DecemberJanuary 20082025}}
{{Html series}}
 
'''Dynamic HTML''', or '''DHTML''', is a collectionterm ofwhich technologieswas used togetherby tosome createbrowser interactivevendors andto animateddescribe [[website]]s by using athe combination of a static [[markup languageHTML]], [[Style sheet (suchweb asdevelopment)|style [[HTMLsheet]]),s aand [[clientDynamic web page#Client-side scripting|client-side scripts]] language (such as [[JavaScript]]), a[[VBScript]], presentationor definitionany languageother (suchsupported asscripts) [[Cascadingthat Styleenabled Sheets|CSS]]),the creation of interactive and theanimated [[documents.<ref>{{Cite web|title=Document Object Model]] (FAQ|url=https://www.w3.org/DOM)/faq.html#DHTML-DOM|access-date=2022-02-16|website=W3C |date=October 22, 2003}}</ref><ref>{{cite web|url=http://www.w3.org/Style/#dynamic|title=Web Style Sheets|website=www.w3.orgW3C|access-date=7 April 2018 |date=22 July 1999}}</ref> The application of DHTML was introduced by [[Microsoft]] with the release of [[Internet Explorer 4]] in 1997.<ref>{{Cite web |date=2020-07-19 |title=DHTML {{!}} A Quick Glance of DHTML with Components, Features, Need |url=https://www.educba.com/dhtml/ |access-date=2022-10-13 |website=EDUCBA |language=en-US |first=Priya |last=Pedamkar}}</ref>{{Unreliable source?|date=November 2022}}
 
DHTML (Dynamic HTML) allows scripting languages, such as JavaScript, to changemodify [[variablevariables (programming)|variable]]sand elements in a web page's definition languagestructure, which in turn affectsaffect the look, behavior, and functionfunctionality of otherwise "static" HTML page content, after the page has been fully loaded and during the viewing process. Thus the dynamic characteristic of DHTML is the way it functions while a page is viewed, not in its ability to generate a unique page with each page load.
 
By contrast, a [[dynamic web page]] is a broader concept, covering any web page generated differently for each user, load occurrence, or specific variable values. This includes pages created by client-side scripting, and ones created by [[server-side scripting]] (such as [[PHP]], [[Python (programming language)|Python]], [[JavaServer Pages|JSP]] or [[ASP.NET]]) where the [[web server]] generates content before sending it to the client.
 
DHTML is differentiatedthe frompredecessor of [[Ajax (programming)|Ajax]] by the fact that aand DHTML pagepages isare still request/reload-based. WithUnder the DHTML model, there may not be any interaction between the client and server after the page is loaded; all processing happens in JavaScript on the client side. By contrast, an Ajax page usesextends features of DHTML to allow the page to initiate anetwork requestrequests (or 'subrequest') to the server even after page load to perform additional actions. For example, if there are multiple [[Tab (interface)|tabs]] on a page, the pure DHTML approach would load the contents of all tabs and then dynamically display only the one that is active, while AJAX could load each tab only when it is really needed.
 
== Uses ==
 
DHTML allows authors to add effects to their pages that are otherwise difficult to achieve, by changing the [[Document Object Model]] (DOM) and page style. The combination of HTML, CSS, and JavaScript offers ways to:
 
* Animate text and images in their document.
Line 20 ⟶ 21:
* Include rollover buttons or drop-down menus.
 
A less common use is to create browser-based action games. Although a number of games were created using DHTML during the late 1990s and early 2000s,<ref>{{CitationCite web |title=Fun and Games With DHTML |first=Stephen |last=Downes needed|date=October 2008 Aug 18, 1999 |url=https://www.downes.ca/post/276 |access-date=2022-08-27 |website=Stephen's Web |language=en}}</ref> differences between browsers made this difficult: many techniques had to be implemented in code to enable the games to work on multiple platforms. Recently browsersBrowsers have beensince convergingthen towardsconverged toward [[web standards]], which has made the design of DHTML games more viable. Those games can be played on all major browsers and theyin can also be ported to [[KDE Plasma Workspaces|Plasma]] for [[KDE]], Widgets for [[macOS]]desktop and Gadgetsdevice forapplications [[Windowsthat Vista]],support whichembedded arebrowser based on DHTML codecontexts.
 
The term "DHTML" has fallen out of use in recent years as it was associated with practices and conventions that tended to not work well between various web browsers.<ref>{{Citationcite book |last1=Ferguson |first1=Russ |last2=Heilmann |first2=Christian |title=Beginning JavaScript with DOM Scripting and Ajax needed|date=September2013 |publisher=Berkeley, CA: Apress |pages=49–68 |doi=10.1007/978-1-4302-5093-7 |isbn=978-1-4302-5092-0 |s2cid=20526670 |url=https://link-springer-com.huaryu.kl.oakland.edu/content/pdf/10.1007/978-1-4302-5093-7.pdf |access-date=May 30, 20212022}}</ref>
 
DHTML support with extensive DOM access was introduced with [[Internet Explorer 4.0]]. Although there was a basic dynamic system with [[Netscape Navigator|Netscape Navigator 4.0]], not all HTML elements were represented in the DOM. When DHTML-style techniques became widespread, varying degrees of support among web browsers for the technologies involved made them difficult to develop and [[debug]]. Development became easier when [[Internet Explorer 5|Internet Explorer 5.0+]], [[Firefox|Mozilla Firefox]] 2.0+, and [[Opera (web browser)|Opera]] 7.0+ adopted a shared [[Document Object Model|DOM]] inherited from [[ECMAScript]].
 
More recentlyLater, [[JavaScript library|JavaScript libraries]] such as [[jQuery]] have abstracted away many of the day-to-day difficulties in cross-browser DOM manipulation, though better standards compliance among browsers has reduced the need for this.
 
== Structure of a web page ==
Line 32 ⟶ 33:
 
Typically a web page using DHTML is set up in the following way:
<syntaxhighlight lang="html5html">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>DHTML example</title>
</head>
<body bgcolor="red">
</script>
 
<script>function init() {
functionlet initmyObj = document.getElementById("navigation") {;
// ... varmanipulate myObj = document.getElementById("navigation");
// ... manipulate myObj}
window.onload = }init;
</bodyscript>
window.onload = init;
</script>!--
Often the code is stored in an external file; this is done
 
by linking the file that contains the JavaScript.
<!--
This is helpful when several pages use the same script:
Often the code is stored in an external file; this is done
-->
by linking the file that contains the JavaScript.
<script src="myjavascriptmy-javascript.js"></script>
This is helpful when several pages use the same script:
</body>
-->
<script src="myjavascript.js"></script>
</body>
</html>
</syntaxhighlight>
Line 62 ⟶ 61:
 
The following code illustrates an often-used function. An additional part of a web page will only be displayed if the user requests it.
<syntaxhighlight lang="html5html">
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Using a DOM function</title>
<style>
a { background-color: #eee; }
a:hover { background: #ff0; }
#toggleMe { background: #cfc; display: none; margin: 30px 0; padding: 1em; }
</style>
</head>
<body>
<h1>Using a DOM function</h1>
<h2><a id="showhide" href="#">Show paragraph</a></h2>
<p id="toggleMetoggle-me">This is the paragraph that is only displayed on request.</p>
<p>The general flow of the document continues.</p>
<script>
function changeDisplayState(iddisplayElement, textElement) {
if (displayElement.style.display === "none" || displayElement.style.display === "") {
var d = document.getElementById('showhide'),
edisplayElement.style.display = document.getElementById(id)"block";
if (etextElement.style.displayinnerHTML === 'none' || e.style.display === '')"Hide {paragraph";
} else e.style.display = 'block';{
ddisplayElement.innerHTMLstyle.display = 'Hide paragraph'"none";
}textElement.innerHTML else= {"Show paragraph";
e.style.display = 'none';
d.innerHTML = 'Show paragraph';
}
}
<!--}
document.getElementById('showhide').addEventListener('click', function (e) {
e.preventDefault();
let displayElement = changeDisplayStatedocument.getElementById('toggleMe'"toggle-me");
let textElement = }document.getElementById("showhide");
document.getElementById('showhide')textElement.addEventListener('"click'", function (eevent) {
</script>
event.preventDefault();
</body>
changeDisplayState(displayElement, textElement);
});
</script>
</body>
</html>
</syntaxhighlight>
Line 106:
==Document Object Model==
{{main|Document Object Model}}
DHTML is not a technology in and of itself; rather, it is the product of three related and complementary technologies: HTML, Cascading Style Sheets (CSS), and [[JavaScript]]. To allow scripts and components to access features of HTML and CSS, the contents of the document are represented as objects in a programming model known as the [[Document Object Model]] (DOM).
 
The DOM API is the foundation of DHTML, providing a structured interface that allows access and manipulation of virtually anything in the document. The HTML elements in the document are available as a hierarchical [[tree (data structure)|tree]] of individual objects, making it possible to examine and modify an element and its attributes by reading and setting properties and by calling methods. The text between elements is also available through DOM properties and methods.
Line 120:
Inline styles are CSS style assignments that have been applied to an element using the style attribute. You can examine and set these styles by retrieving the style object for an individual element. For example, to highlight the text in a heading when the user moves the mouse pointer over it, you can use the style object to enlarge the font and change its color, as shown in the following simple example.
 
<syntaxhighlight lang="html5html">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Dynamic Styles</title>
<style>
ul { display: none; }
</style>
</head>
 
<body>
<h1 id="firstHeaderfirst-header">Welcome to Dynamic HTML</h1>
 
<p><a id="clickableLinkclickable-link" href="#">Dynamic styles are a key feature of DHTML.</a></p>
 
<ul id="unorderedListunordered-list">
<li>Change the color, size, and typeface of text</li>
<li>Show and hide text</li>
<li>And much, much more</li>
</ul>
 
<p>We've only just begun!</p>
 
<script>
function showMe() {
document.getElementById("firstHeaderfirst-header").style.color = "#990000";
document.getElementById("unorderedListunordered-list").style.display = "block";
}
 
document.getElementById("clickableLinkclickable-link").addEventListener("click", function (eevent) {
eevent.preventDefault();
showMe();
});
</script>
</body>
</html>
</syntaxhighlight>
Line 167:
== External links ==
{{Sister project|project=wikibooks|text=JavaScript/DHTML}}
* [http://www.quirksmode.org/ QuirksMode], a comprehensive site with test examples and instructions on how to write DHTML code whichthat runs on several browsers.
* [http://www.yourhtmlsource.com/javascript/dhtmlexplained.html Introductory DHTML Tutorial]