3

I'm looking for a way to grab all hyperlinks from DOM. and save it to array or variables.(for chrome extension).

Any help would be appreciated.

Thanks

var myNodelist = document.querySelectorAll("a");
var URLList;
for (i = 0; i < myNodelist.length; i++) {
   URLList[i]=document.getElementById("a").innerHTML;
}
Gundalai
  • 33
  • 4
  • 2
    URLList is `undefined` ... you can't add properties to it ... try `var URLList = []` – Bravo Nov 04 '19 at 09:30
  • Possible duplicate: https://stackoverflow.com/questions/897980/how-to-extract-javascript-links-in-an-html-document – Robin B Nov 04 '19 at 09:30
  • 1
    @RobinB - doesn't look likely – Bravo Nov 04 '19 at 09:31
  • 1
    Also ... `getElementById('a')` will always get the element whose id is `a` - nothing to do with `myNodeList` ... – Bravo Nov 04 '19 at 09:33
  • 1
    And one final thing ... `innerHTML` is the displayed content of the `a` tag, not the link itself – Bravo Nov 04 '19 at 09:35
  • [`document.links`](https://html.spec.whatwg.org/multipage/dom.html#dom-document-links) is sufficient enough. It is as basic as can be ... it's "DOM-Level-2". This HTMLCollection is a live-collection that exclusively exposes anchor and/or area-elements that ***do*** have/feature a `href` attribute. Thus there is no need to filter anything from this collection. For a more comfortable iteration of such a structure just transform it like that ... `Array.from(document.links)`. – Peter Seliger Nov 04 '19 at 10:43

3 Answers3

0

You can get this as below.

var arr = [], l = document.links;
for(var i=0; i<l.length; i++) {
  arr.push(l[i].href);
}
console.log(arr);
<a href="demo1.html">Hello</a>
<a href="demo1.html">Hello</a>
<a href="demo1.html">Hello</a>

Reference: https://stackoverflow.com/a/3871370/10971575

Pushprajsinh Chudasama
  • 7,772
  • 4
  • 20
  • 43
0

You can do like this

var myNodelist = document.querySelectorAll("a");
var URLList = [];
for (var i = 0; i < myNodelist.length; i++) {
  if (myNodelist[i].href) {
    URLList.push(myNodelist[i].href);
  }
}
console.log(URLList)
Linh Nguyen
  • 925
  • 1
  • 10
  • 23
  • This approach does not take [`area` elements](https://html.spec.whatwg.org/multipage/image-maps.html#the-area-element) into account. – Peter Seliger Nov 04 '19 at 11:30
0

Please check following snippet
get all the link using java scripts as Verified solution Fastest solution

var HyperlinkArray = document.getElementsByTagName('a');

for(var i = 0; i< HyperlinkArray.length; i++){
  console.log(HyperlinkArray[i].href);//List of Hyperlink
}

Example :

var HyperlinkArray = document.getElementsByTagName('a');

for(var i = 0; i< HyperlinkArray.length; i++){
  console.log(HyperlinkArray[i].href);//List of Hyperlink
}
<a href="https://twitter.com/stackoverflow">twitter</a><br>    
<a href="https://scicomp.stackexchange.com">scicomp</a><br>
<a href="https://biology.stackexchange.com">biology</a><br>
<a href="https://stackapps.com">stackapps</a><br>
<a href="https://mathoverflow.net">mathoverflow</a><br>
  • This approach does not take [`area` elements](https://html.spec.whatwg.org/multipage/image-maps.html#the-area-element) into account. – Peter Seliger Nov 04 '19 at 11:31
  • Please clarify your requirement @PeterSeliger – Hardik Masalawala Nov 04 '19 at 11:40
  • Hypertext references are not just exclusively represented by anchor elements that do feature a `href` attribute. My comment provides a link to another element that also is capable of representing a link. Thus your approach does not provide a fully generic solution. On the other hand [`document.links`](https://html.spec.whatwg.org/multipage/dom.html#dom-document-links) already is sufficient enough. It is as basic as can be ... it's "DOM-Level-2". This HTMLCollection is a live-collection that exclusively exposes anchor and/or area-elements that do have/feature a href attribute. – Peter Seliger Nov 04 '19 at 11:45