JAVA SCRIPT - Accessing All Images in the Web Page - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript JAVA SCRIPT - Accessing All Images in the Web Page - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript


Post Top Ad

Post Top Ad

Thursday, December 27, 2018

JAVA SCRIPT - Accessing All Images in the Web Page

 Accessing All Images in the Web Page


You want to access all img elements in a given document.


Use the document.getElementsByTagName() method, passing in img as the parameter:
var imgElements = document.getElementsByTagName('img');


The document.getElementsByTagName() method returns a collection of nodes (a NodeList) of a given element type, such as the img tag in the solution. The collection can be traversed like an array, and the order of nodes is based on the order of the elements within the document (the first img element in the page is accessible at index 0, etc.):

var imgElements = document.getElementsByTagName('img');
for (let i = 0; i < imgElements.length; i++) {
 var img = imgElements[i];

The NodeList collection can be traversed like an array, but it isn’t an Array object—you can’t use Array object methods, such as push() and reverse(), with a NodeList. Its only property is length, and its only method is item(), returning the element at the position given by an index passed in as parameter:

var img = imgElements.item(1); // second image

NodeList is an intriguing object because it’s a live collection, which means changes made to the document after the NodeList is retrieved are reflected in the collection. demonstrates the NodeList live collection functionality, as well as getEle mentsByTagName.

Three images in the web page are accessed as a NodeList collection using the getElementsByTagName method. The length property, with a value of 3, is output to the console. Immediately after, a new paragraph and img elements are created, and the img is appended to the paragraph. To append the paragraph following the others in the page, getElementsByTagName is used again, this time with the paragraph tags (p).

We’re not really interested in the paragraphs, but in the paragraphs’ parent elements, found via the parentNode property on each paragraph. The new paragraph element is appended to the paragraph’s parent element, and the previously accessed NodeList collection’s length property is again printed out. Now, the value is 4, reflecting the addition of the new img element.

 Demonstrating getElementsByTagName and the NodeList live collection property

<!DOCTYPE html>
 <p><img src="firstimage.jpg" alt="image description" /></p>
 <p><img src="secondimage.jpg" alt="image description" /></p>
 <p><img src="thirdimage.jpg" alt="image description" /></p>
 var imgs = document.getElementsByTagName('img');
 var p = document.createElement("p");
 var img = document.createElement("img");
 var paras = document.getElementsByTagName('p');

In addition to using getElementsByTagName() with a specific element type, you can also pass the universal selector (*) as a parameter to the method to get all elements:

var allelems = document.getElementsByTagName('*');

In the code demonstrated in the discussion, the children nodes are traversed using a traditional for loop. Array functionality, such as forEach(), can’t be used directly with a NodeList because it’s not an Array. You can coerce the NodeList, as is demonstrated in, but this type of coercion has its own drawbacks.

 Namespace Variation

Like most of the DOM API access methods, there is a variation of getElementsByTag Name(), getElementsByTagNameNS, which can be used in documents that support mul‐ tiple namespaces, such as an XHTML web page with embedded MathML or SVG.

 SVG document is embedded in XHTML. Both the XHTML docu‐ ment and the embedded SVG make use of the title element. The title element in the XHTML document is part of the default XHTML namespace, but the title in the SVG is part of the Dublin Core namespace.

When the title element is accessed, information about the title, including its name‐ space, the prefix, the localName, and the textContent, are printed out. The prefix is the dc component of dc:title, and the localName is the title part of dc:title. The textContent is a new property, added with the DOM Level 2, and is the text of the element. In the case of title (either the XHTML or the Dublin Core element), it would be the title text.

The differences between the namespace and nonnamespace variation of getElementsByTagName 

<!DOCTYPE html>
<html xmlns="" xml:lang="en">
<script type="text/javascript">
window.onload=function () {
 var str = "";
 var title = document.getElementsByTagName("title");
 for (var i = 0; i < title.length; i++) {
 str += title.item(i).namespaceURI + " " +
 title.item(i).prefix + " " +
 title.item(i).localName + " " +
 title.item(i).text + " ";
 str = "";
 if (!document.getElementsByTagNameNS) return;
 var titlens =
 for (var i = 0; i < titlens.length; i++) {
 str += titlens.item(i).namespaceURI + " " +
 titlens.item(i).prefix + " " +
 titlens.item(i).localName + " " +
 titlens.item(i).textContent + " ";
<svg id="svgelem" height="800" width="800" xmlns="">
 <circle id="redcircle" cx="300" cy="300" r="300"
 fill="red" />
 <rdf:RDF xmlns:cc=""
 <cc:Work rdf:about="">
 <dc:title>Sizing Red Circle</dc:title>
<cc:Agent rdf:about="">
 <dc:title>Testing RDF in SVG</dc:title>
 <dc:title id="title">Testing</dc:title>

The result of the application can vary between browsers. When using Firefox and later
versions of IE, and accessing title using getElementsByTagName(), the only title returned is the XHTML document title.

 However, when using the namespace varia‐ tion (getElementsByTagNameNS()), and specifying the Dublin Core namespace, all of the Dublin Core titles in the RDF within the SVG are returned.

When using getElementsByTagName with title in Chrome and Opera, both the XHTML title and the Dublin Core titles are returned,

Though IE8 doesn’t directly support the XHTML MIME type, if the page is served as text/html using some form of content negotiation, IE will process the page as HTML. However, though the getElementsByTagName() works with IE, the namespaced version of the method, getElementsByTagNameNS(), does not. All of the values are returned as undefined. IE8 doesn’t return the dc:title entries in the SVG, either. If the Dublin Core namespace is declared in the html element, instead of in the svg element, IE8 does return all of the dc:title entries, as well as the XHTML title:

An alternative approach uses a tag name string that concatenates the prefix and localName. All of the browsers will find the dc:title using the following:

var titles = document.getElementsByTagName("dc:title");

No comments:

Post a Comment

Post Top Ad