Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 667001 of Examples and demos from articles

  • Revision slug: Mozilla/Add-ons/Code_snippets/From_articles
  • Revision title: Examples and demos from articles
  • Revision id: 667001
  • Created:
  • Creator: fusionchess
  • Is current revision? Yes
  • Comment Added examples

Revision Content

What follows is a brief list of examples and demos from our theoretical articles.

Live demos

JavaScript

Rich-Text Editor [zip]
How to standardize the creation of complete Rich-Text Editors in web pages. [article]
Typewriter effect [html]
The following example will delete and re-type simulating a typewriter all the text content of the NodeList which match a specified group of selectors. [article]
Fade In/Out [html]
Sometimes a page uses dozens and dozens of animations. In such a condition is difficult and unnatural to keep track of all events started and then to stop them when appropriate through the clearTimeout() function. A possible approach to solve this problem is to nest all the informations needed by each animation to start, stop, etc. etc. in different objects and then to create a constructor for such class of objects in order to standardize and simplify the instantiation of them.
Here is a possible and minimalist example of such abstraction, which for this reason we named MiniDaemon. [article]
Image preview before upload [html]
The FileReader.prototype.readAsDataURL() method is useful, for example, to get a preview of an image before uploading it. [article]
Note: The FileReader() constructor was not supported by Internet Explorer for versions before 10. For a full compatibility code, see also our crossbrowser possible solution for image preview.
Determine if an element has been totally scrolled [html]
This example shows how to determine whether a user has completely scrolled an element or not. [article]

CSS

Pure-CSS dropdown menu [html]
With the :hover pseudo-class you can create complex cascade algorithms. This is a common technique used, for example, in order to create pure-CSS dropdown menus (that is only CSS, without using JavaScript). .[article]
Pure-CSS web gallery based on the :hover pseudoclass [zip]
You can use the :hover pseudo-class in order to build an image gallery with full-size images shown only when mouse goes over previews. .[article]
Pure-CSS web gallery based on the :checked pseudoclass [zip]
You can also use the :checked pseudo-class applied to hidden radioboxes in order to build, for example, an image gallery with full-size images shown only when mouse clicks on previews. [article]
Pure-CSS tooltips [html]
This shows the use of the ::after pseudo-element in conjunction with the attr() CSS expression and a data-descr custom data attribute to create a pure-CSS, glossary-like tooltip. [article]
Pure-CSS lightbox [html]
The :target pseudo-class is useful to switch on/off some invisible elements. In this way you can create a pure-CSS lightbox. [article]
Pure-CSS editable (pseudo)<select> [html]
This example shows how it's possible to simulate an editable {{HTMLElement("select")}} through a fieldset of radioboxes and textboxes. [article]
Expandable elements [html]
This example shows how it is possible to show/hide classes of elements in pure CSS. In this demo there is also an example of a "toggle button" rendered through a {{ HTMLElement("label") }} element. [article]

Code snippets and tutorials

JavaScript

Complete cookies reader/writer with full unicode support
This little framework consists of a complete cookies reader/writer with unicode support.
XML to JSON conversion snippets (JXON)
JXON (lossless JavaScript XML Object Notation) is a generic name by which is defined the representation of JavaScript Objects using XML. There are no real standards for this conversion, but some conventions begin to appear on the web.
Autogrowing <textarea>
This example shows how to make a textarea really autogrowing during a typing.
Insert some custom text in a <textarea>
This example shows how to insert some HTML tags or smiles or any custom text in a textarea.
Using setInterval within animations
Sometimes a page uses dozens and dozens of animations. In such a condition is difficult and unnatural to keep track of all events started and then to stop them when appropriate through the clearTimeout() function. A possible approach to solve this problem is to nest all the informations needed by each animation to start, stop, etc. etc. in different objects and then to create a constructor for such class of objects in order to standardize and simplify the instantiation of them.
Convert relative paths to absolute
This example show how to translate relative paths to absolute paths.
A shortcut to massively modify the DOM
This example shows how to create a framework able to modify many properties of DOM objects at the same time.
Filter the digitation into a form field, Capture the digitation of a hidden word
This example shows the use of the onkeypress event during a digitation into a form field.
AJAX – Submitting forms and uploading files
This paragraph shows how to submit forms in pure-AJAX.
Multipage AJAX navigation example
This article provides a working (minimalist) example of a pure-AJAX web site composed only of three pages.
Do something if current document has changed since last visit
Here is a possible example of how to show an alert message when current document changes.
Do something if an external page has changed since last visit
Here is a possible example of how to show an alert message when an external page changes.
Import scripts
Here is a possible example of how to dynamically import scripts with JavaScript.

CSS

CSS Tutorial
Aimed at complete beginners, this CSS tutorial for beginners introduces you to Cascading Style Sheets (CSS). It guides you through the basic features of the language with practical examples that you can try for yourself on your own computer and illustrates the standard features of CSS that work in modern browsers.
Dropdown menus rules
How to create pure-CSS dropdown menus.

See also

Revision Source

<p>What follows is a brief list of examples and demos from our theoretical articles.</p>
<h2 id="Live_demos">Live demos</h2>
<h3 id="JavaScript">JavaScript</h3>
<dl>
 <dt>
  <a href="https://mdn.mozillademos.org/files/3703/rich-text-editor.zip" title="https://mdn.mozillademos.org/files/3703/rich-text-editor.zip">Rich-Text Editor</a> <sup>[zip]</sup></dt>
 <dd>
  How to standardize the creation of complete Rich-Text Editors in web pages. <sup>[<a href="/en-US/docs/Rich-Text_Editing_in_Mozilla#Example.3A_a_simple_but_complete_Rich_Text_Editor" title="/en-US/docs/Rich-Text_Editing_in_Mozilla#Example.3A_a_simple_but_complete_Rich_Text_Editor">article</a>]</sup></dd>
 <dt>
  <a href="https://mdn.mozillademos.org/files/3997/typewriter.html" title="https://mdn.mozillademos.org/files/3997/typewriter.html">Typewriter effect</a> <sup>[html]</sup></dt>
 <dd>
  The following example will delete and re-type simulating a <em>typewriter</em> all the text content of the NodeList which match a specified group of selectors. <sup>[<a href="/en-US/docs/Web/API/window.setInterval#Example_3.3A_Simulating_a_typewriter" title="/en-US/docs/Web/API/window.setInterval#Example_3.3A_Simulating_a_typewriter">article</a>]</sup></dd>
 <dt>
  <a href="https://mdn.mozillademos.org/files/3995/minidaemon_example.html" title="https://mdn.mozillademos.org/files/3995/minidaemon_example.html">Fade In/Out</a> <sup>[html]</sup></dt>
 <dd>
  Sometimes a page uses dozens and dozens of animations. In such a condition is difficult and unnatural to keep track of all events started and then to stop them when appropriate through the <code>clearTimeout()</code> function. A possible approach to solve this problem is to nest all the informations needed by each animation to start, stop, etc. etc. in different objects and then to create a constructor for such class of objects in order to standardize and simplify the instantiation of them.<br />
  Here is a possible and minimalist example of such abstraction, which for this reason we named <code><em>MiniDaemon</em></code>. <sup>[<a href="/en-US/docs/DOM/window.setInterval#A_little_framework" title="/en-US/docs/CSS/:hover#Gallery_with_full-size_images_and_previews">article</a>]</sup></dd>
 <dt>
  <a href="https://mdn.mozillademos.org/files/3698/image_upload_preview.html" title="https://mdn.mozillademos.org/files/3698/image_upload_preview.html">Image preview before upload</a> <sup>[html]</sup></dt>
 <dd>
  The <code>FileReader.prototype.readAsDataURL()</code> method is useful, for example, to get a preview of an image before uploading it. <sup>[<a href="/en-US/docs/Web/API/FileReader.readAsDataURL" title="/en-US/docs/CSS/:hover#Gallery_with_full-size_images_and_previews">article</a>]</sup>
  <div class="note">
   <strong>Note:</strong> The <a href="https://developer.mozilla.org/en-US/docs/Web/API/FileReader"><code>FileReader()</code></a> constructor was not supported by Internet Explorer for versions before 10. For a full compatibility code, see also our <a class="internal external external-icon" href="https://mdn.mozillademos.org/files/3699/crossbrowser_image_preview.html" title="crossbrowser_image_preview.html">crossbrowser possible solution for image preview</a>.</div>
 </dd>
 <dt>
  <a href="https://mdn.mozillademos.org/files/4589/readme-example.html" title="https://mdn.mozillademos.org/files/4589/readme-example.html">Determine if an element has been totally scrolled</a> <sup>[html]</sup></dt>
 <dd>
  This example shows how to determine whether a user has completely scrolled an element or not. <sup>[<a href="/en-US/docs/Web/API/Element.scrollHeight#scrollHeight_Demo" title="/en-US/docs/CSS/:hover#Gallery_with_full-size_images_and_previews">article</a>]</sup></dd>
</dl>
<h4 id="CSS">CSS</h4>
<dl>
 <dt>
  <a href="https://mdn.mozillademos.org/files/3700/css_dropdown_menu.html" title="https://mdn.mozillademos.org/files/3700/css_dropdown_menu.html">Pure-CSS dropdown menu</a> <sup>[html]</sup></dt>
 <dd>
  With the <code>:hover</code> pseudo-class you can create complex cascade algorithms. This is a common technique used, for example, in order to create <strong>pure-CSS dropdown menus</strong> (that is only CSS, without using <a href="https://developer.mozilla.org/en/JavaScript" title="en/JavaScript">JavaScript</a>). <sup>.[<a href="/en-US/docs/CSS/:hover#Dropdown_menu" title="/en-US/docs/CSS/:hover#Gallery_with_full-size_images_and_previews">article</a>]</sup></dd>
 <dt>
  <a href="https://mdn.mozillademos.org/files/3707/css-gallery.zip" title="https://mdn.mozillademos.org/files/3707/css-gallery.zip">Pure-CSS web gallery based on the <code>:hover</code> pseudoclass</a> <sup>[zip]</sup></dt>
 <dd>
  You can use the <code>:hover</code> pseudo-class in order to build an image gallery with full-size images shown only when mouse goes over previews. <sup>.[<a href="/en-US/docs/CSS/:hover#Gallery_with_full-size_images_and_previews" title="/en-US/docs/CSS/:hover#Gallery_with_full-size_images_and_previews">article</a>]</sup></dd>
 <dt>
  <a href="https://mdn.mozillademos.org/files/3723/css-checked-gallery.zip" title="https://mdn.mozillademos.org/files/3723/css-checked-gallery.zip">Pure-CSS web gallery based on the <code>:checked</code> pseudoclass</a> <sup>[zip]</sup></dt>
 <dd>
  You can also use the <code>:checked</code> pseudo-class applied to hidden radioboxes in order to build, for example, an image gallery with full-size images shown only when mouse clicks on previews. <sup>[<a href="/en-US/docs/CSS/:hover#Gallery_with_full-size_images_and_previews" title="/en-US/docs/CSS/:hover#Gallery_with_full-size_images_and_previews">article</a>]</sup></dd>
 <dt>
  <a href="https://mdn.mozillademos.org/files/4591/css-only_tooltips.html" title="https://mdn.mozillademos.org/files/4591/css-only_tooltips.html">Pure-CSS <em>tooltips</em></a> <sup>[html]</sup></dt>
 <dd>
  This shows the use of the <code>::after</code> <a href="https://developer.mozilla.org/en/CSS/Pseudo-elements" title="Pseudo-elements">pseudo-element</a> in conjunction with the <a href="https://developer.mozilla.org/en-US/docs/CSS/attr" title="/en-US/docs/CSS/attr"><code>attr()</code></a> CSS expression and a <code>data-descr</code> <a href="https://developer.mozilla.org/en/HTML/Global_attributes#attr-data-*" title="en/HTML/Global_attributes#attr-data-*">custom data attribute</a> to create a pure-CSS, glossary-like <em>tooltip</em>. <sup>[<a href="/en-US/docs/CSS/::after#Tooltips" title="/en-US/docs/CSS/:hover#Gallery_with_full-size_images_and_previews">article</a>]</sup></dd>
 <dt>
  <a href="https://mdn.mozillademos.org/files/4607/lightbox.html" title="https://mdn.mozillademos.org/files/4607/lightbox.html">Pure-CSS <em>lightbox</em></a> <sup>[html]</sup></dt>
 <dd>
  The <code>:target</code> pseudo-class is useful to switch on/off some invisible elements. In this way you can create a pure-CSS lightbox. <sup>[<a href="/en-US/docs/CSS/:target#Creating_a_pure_CSS_.22lightbox.22" title="/en-US/docs/CSS/:hover#Gallery_with_full-size_images_and_previews">article</a>]</sup></dd>
 <dt>
  <a href="https://mdn.mozillademos.org/files/4563/editable_select.html" title="https://mdn.mozillademos.org/files/4563/editable_select.html">Pure-CSS editable (pseudo)<code>&lt;select&gt;</code></a> <sup>[html]</sup></dt>
 <dd>
  This example shows how it's possible to simulate an editable {{HTMLElement("select")}} through a <code>fieldset</code> of <a href="/en-US/docs/HTML/Element/Input" title="en-US/docs/HTML/Element/Input">radioboxes</a> and <a href="/en-US/docs/HTML/Element/Input" title="en-US/docs/HTML/Element/Input">textboxes</a>. <sup>[<a href="/en-US/docs/HTML/Element/fieldset#Example_.232.3A_Simulating_an_editable_&lt;select&gt;_through_a_fieldset_of_radioboxes_and_textboxes" title="/en-US/docs/CSS/:hover#Gallery_with_full-size_images_and_previews">article</a>]</sup></dd>
 <dt>
  <a href="https://mdn.mozillademos.org/files/3706/expandable-elements.html" title="https://mdn.mozillademos.org/files/3706/expandable-elements.html">Expandable elements</a> <sup>[html]</sup></dt>
 <dd>
  This example shows how it is possible to show/hide classes of elements in pure CSS. In this demo there is also an example of a "toggle button" rendered through a {{ HTMLElement("label") }} element. <sup>[<a href="/en-US/docs/Web/CSS/:checked#Using_hidden_checkboxes_in_order_to_store_some_CSS_boolean_values" title="/en-US/docs/Web/CSS/:checked#Using_hidden_checkboxes_in_order_to_store_some_CSS_boolean_values">article</a>]</sup></dd>
</dl>
<h2 id="Code_snippets_and_tutorials">Code snippets and tutorials</h2>
<h3 id="JavaScript_2">JavaScript</h3>
<dl>
 <dt>
  <a href="/en-US/docs/DOM/document.cookie#A_little_framework.3A_a_complete_cookies_reader.2Fwriter_with_full_unicode_support" title="/en-US/docs/DOM/document.cookie#A_little_framework.3A_a_complete_cookies_reader.2Fwriter_with_full_unicode_support">Complete cookies reader/writer with full unicode support</a></dt>
 <dd>
  This little framework consists of a complete cookies reader/writer with unicode support.</dd>
 <dt>
  <a href="/en-US/docs/JXON" title="/en-US/docs/JXON">XML to JSON conversion snippets (JXON)</a></dt>
 <dd>
  JXON (lossless <strong>J</strong>avaScript <strong>X</strong>ML <strong>O</strong>bject <strong>N</strong>otation) is a generic name by which is defined the representation of JavaScript Objects using <a href="/en-US/docs/XML" title="en/XML">XML</a>. There are no real standards for this conversion, but <a href="/en-US/docs/JXON#The_Parker_Convention" title="The Parker Convention">some conventions begin to appear on the web</a>.</dd>
 <dt>
  <a href="/en-US/docs/DOM/HTMLTextAreaElement#Example_.231.3A_Autogrowing_textarea" title="/en-US/docs/DOM/HTMLTextAreaElement#Example_.231.3A_Autogrowing_textarea">Autogrowing <code>&lt;textarea&gt;</code></a></dt>
 <dd>
  This example shows how to make a textarea really autogrowing during a typing.</dd>
 <dt>
  <a href="/en-US/docs/DOM/HTMLTextAreaElement#Example_.232.3A_Insert_HTML_tags" title="/en-US/docs/DOM/HTMLTextAreaElement#Example_.232.3A_Insert_HTML_tags">Insert some custom text in a <code>&lt;textarea&gt;</code></a></dt>
 <dd>
  This example shows how to insert some HTML tags or <em>smiles</em> or any custom text in a textarea.</dd>
 <dt>
  <a href="/en-US/docs/DOM/window.setInterval#A_little_framework" title="/en-US/docs/DOM/window.setInterval#A_little_framework">Using <code>setInterval</code> within animations</a></dt>
 <dd>
  Sometimes a page uses dozens and dozens of animations. In such a condition is difficult and unnatural to keep track of all events started and then to stop them when appropriate through the <code>clearTimeout()</code> function. A possible approach to solve this problem is to nest all the informations needed by each animation to start, stop, etc. etc. in different objects and then to create a constructor for such class of objects in order to standardize and simplify the instantiation of them.</dd>
 <dt>
  <a href="/en-US/docs/Web/API/document.cookie#Using_relative_URLs_in_the_path_parameter" title="/en-US/docs/Web/API/document.cookie#Using_relative_URLs_in_the_path_parameter">Convert relative paths to absolute</a></dt>
 <dd>
  This example show how to translate <em>relative</em> paths to <em>absolute</em> paths.</dd>
 <dt>
  <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Function#Example" title="/en-US/docs/JavaScript/Reference/Global_Objects/Function#Example">A shortcut to massively modify the DOM</a></dt>
 <dd>
  This example shows how to create a framework able to modify many properties of DOM objects at the same time.</dd>
 <dt>
  <a href="/en-US/docs/Web/API/GlobalEventHandlers.onkeypress#Example_1.3A_Filter_the_digitation_in_a_form_field_through_a_regular_expression" title="/en-US/docs/Web/API/GlobalEventHandlers.onkeypress#Example_1.3A_Filter_the_digitation_in_a_form_field_through_a_regular_expression">Filter the digitation into a form field</a>, <a href="/en-US/docs/Web/API/GlobalEventHandlers.onkeypress#Example_2.3A_Capture_the_digitation_of_a_hidden_word" title="/en-US/docs/Web/API/GlobalEventHandlers.onkeypress#Example_2.3A_Capture_the_digitation_of_a_hidden_word">Capture the digitation of a hidden word</a></dt>
 <dd>
  This example shows the use of the <code>onkeypress</code> event during a digitation into a form field.</dd>
 <dt>
  <a href="/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Submitting_forms_and_uploading_files" title="/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Submitting_forms_and_uploading_files">AJAX – Submitting forms and uploading files</a></dt>
 <dd>
  This paragraph shows how to submit forms in <em>pure-AJAX</em>.</dd>
 <dt>
  <a href="/en-US/docs/Web/Guide/DOM/Manipulating_the_browser_history/Example" title="/en-US/docs/Web/Guide/DOM/Manipulating_the_browser_history/Example">Multipage AJAX navigation example</a></dt>
 <dd>
  This article provides a working (minimalist) example of a <em>pure-AJAX</em> web site composed only of three pages.</dd>
 <dt>
  <a href="/en-US/docs/Web/API/document.lastModified#Notes" title="/en-US/docs/Web/API/document.lastModified#Notes">Do something if <em>current document</em> has changed since last visit</a></dt>
 <dd>
  Here is a possible example of how to show an alert message when current document changes.</dd>
 <dt>
  <a href="/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Do_something_when_last_modified_date_changes" title="/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Do_something_when_last_modified_date_changes">Do something if <em>an external page</em> has changed since last visit</a></dt>
 <dd>
  Here is a possible example of how to show an alert message when an external page changes.</dd>
 <dt>
  <a href="/en-US/docs/Web/API/HTMLScriptElement#Dynamically_importing_scripts" title="/en-US/docs/Web/API/HTMLScriptElement#Dynamically_importing_scripts">Import scripts</a></dt>
 <dd>
  Here is a possible example of how to dynamically import scripts with JavaScript.</dd>
</dl>
<h3 id="CSS_2">CSS</h3>
<dl>
 <dt>
  <a href="/en-US/docs/CSS/Getting_Started" title="/en-US/docs/CSS/Getting_Started">CSS Tutorial</a></dt>
 <dd>
  Aimed at complete beginners, this <strong>CSS tutorial</strong> for beginners introduces you to <a href="/en-US/docs/CSS" title="/en-US/docs/CSS">Cascading Style Sheets</a> (CSS). It guides you through the basic features of the language with practical examples that you can try for yourself on your own computer and illustrates the standard features of CSS that work in modern browsers.</dd>
 <dt>
  <a href="/en-US/docs/CSS/:hover#Dropdown_menu" title="/en-US/docs/CSS/:hover#Dropdown_menu">Dropdown menus rules</a></dt>
 <dd>
  How to create pure-CSS dropdown menus.</dd>
</dl>
<h2 id="See_also">See also</h2>
<ul>
 <li><a href="/en-US/docs/Code_snippets" title="/en-US/docs/Code_snippets">Code snippets</a></li>
</ul>
Revert to this revision