简体   繁体   中英

Get all href links in DOM

I need to write code that puts all of the href links from a webpage into an array. Here's what I have so far:

var array = [];
var links = document.links;
for(var i=0; i<links.length; i++) {
  array.push(links[i].href);
}

However, this does not work on a page like Gmail's inbox, because the some of the links are within an iframe. How can I get ALL of the links, including the ones inside the iframe?

Also, this is for a google chrome extension. In the manifest, I have all_frames set to true - does this make a difference?

Thanks

One thing to remember that

  1. document.links
  2. document.images
  3. document.forms
  4. document.forms[0].elements
  5. document.getElementsByName()
  6. document.getElementsByClassName()
  7. document.getElementsByTagName()

are live queries to DOM objects, therefore in forLoops it may significantly slow down your execution (as i < links.length is queries on each for cycle), if you check the array length like this:

var array = [];
var links = document.getElementsByTagName("a");
for(var i=0; i<links.length; i++) {
    array.push(links[i].href);
}

instead you better do this:

var array = [];
var links = document.getElementsByTagName("a");
for(var i=0, max=links.length; i<max; i++) {
    array.push(links[i].href);
}

Surely you're going to get 'arr is not defined' with your code to begin with?

var array = [];
var links = document.links;
for(var i=0; i<links.length; i++) {
    arr.push(links[i].href);
}

Try:

var array = [];
var links = document.getElementsByTagName("a");
for(var i=0; i<links.length; i++) {
    array.push(links[i].href);
}

I have a method I use to access data in an IFrame. How fun that the answer is never just written down to read and use :P. Feel free to modify and abuse:

public HtmlElementCollection GetIFrameElements(String tmpTag, int Frame)
    {
        HtmlElementCollection tmpCollection = mWebBrowser.Document.Window.Frames[Frame].Document.Body.GetElementsByTagName(tmpTag);
        return tmpCollection;
    }

I then use it to look for whatever element Im after:

foreach (HtmlElement el in GetElements("input"))
        {
            if (el.GetAttribute("id").Equals("hasNoGoogleAccount"))
            {
                el.InvokeMember("click");
            }
        }

You could always change the method to loop through and get all iFrames etc blah blah but that should be enough to get you moving. Rate me! Im new

From my Web Adjuster 's bookmarklet code,

function all_frames_docs(c) {
    var f=function(w) {
        if(w.frames && w.frames.length) {
            var i; for(i=0; i<w.frames.length; i++) f(w.frames[i])
        } c(w.document) };
    f(window) }

You can pass any function into all_frames_docs and it will be called in turn on every frame and iframe in the current window, provided your script has access to such (ie it's an extension or a bookmarklet). So all you have to do now is code the function to handle each document, which can go through document.getElementsByTagName("a") or whatever, and make this function the parameter of your call to all_frames_docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM