Contents
Introduction
हेलो दोस्तों आज हम इस post में javascript getElementsByTagNameNS() method के बारे में deails से example के साथ जानेगे!.
Javascript getElementsByTagNameNS() in hindi
Javascript में getElementsByTagNameNS() method का इस्तेमाल web page के सभी elements को एक specific tag
name और namespace के साथ access करने के लिए उपयोग किया जाता है!.
XML documents के साथ काम करते time वह method खास तोर पर उपयोगी होता है!. क्योकि यह आपको एक specific
namespace के साथ elements को search करने की अनुमति देती है!. एक specific tag name vale सभी elements के बजाय!.
syntax:
getElementsByTagNameNS(namespace, name)
यह method दो parameter लेती है!.
- उन elements का namespace URI जिन्हे आप access करना चाहते है!.
- उन elements का tag name जिन्हे आप access करना चाहते है!.
For example, namespace “http://www.library.com” के अंदर “book” tag name वाले सभी elements को
access करने के लिए code
Example:
var books = document.getElementsByTagNameNS("http://www.library.com", "book");
यह एक HTMLcollection object return करेगा जिसमे “http://www.library.com” namespace के अंदर “book” tag name वाले सभी elements शामिल है!. फिर आप array-like syntax जैसे की books[0],books[1] आदि का उपयोग करके collection में अलग-अलग element को access कर सकते है!.
getElementsByTagNameNS() method सभी browser supported नहीं है!. इसलिए browsers को check करना
एक अच्छा विचार है!. यहाँ एक example दिया गया है की app XML document में getElementsByTagNameNS() method का उपयोग कैसे कर सकते है!.
Example:
<library xmlns:book="http://www.library.com"> <book:book> <book:title>The Grapes of Wrath</book:title> <book:author>John Steinbeck</book:author> </book:book> <book:book> <book:title>Animal Farm</book:title> <book:author>George Orwell</book:author> </book:book> </library>
इस example में हमने “http://www.library.com” URI के साथ एक namespace “book” को defined किया है!. इस तरह से हम getElementsByTagNameNS() का इस्तेमाल कर सकते है!. namespace “http://www.library.com” के अंदर “book” tag name के साथ सभी elements access कर सकते है!.
Example:
var xhttp = new XMLHttpRequest(); xhttp.open("GET", "library.xml", true); xhttp.send(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { var xmlDoc = this.responseXML; var books = xmlDoc.getElementsByTagNameNS("http://www.library.com", "book"); for (var i = 0; i < books.length; i++) { console.log(books[i].getElementsByTagName("title")[0].childNodes[0].nodeValue); } } };
इस example में हम XML document “library.xml” लेन के लिए XMLHttpRequest के इस्तेमाल कर रहे है और फिर getElementsByTagNameNS() का उपयोग करने के लिए namespace “http://www.library.com” के अंदर tag name “book” वाले सभी element और book के title को access कर रहे है!.
अंत में getElementsByTagNameNS() method एक XML document में एक specific namespace के अंदर सभी elements को access करने के लिए एक उपयोगी tool है। javascript यह आपको specific elements को target करने और उनके data को retrive करने की अनुमति देता है!. जिससे यह एक valuable जोड़े बन जाता है!.