skip to Main Content

I’m developing tool to check status of all type of resources like js, css, img etc and other urls for given website.

For e.g. Given url: www.abc.com
Then I need to check status of all type of resources and urls for www.abc.com

For that I’m using jquery ajax… So as result, I will get html content of that url (abc.com)

But problem is how find all urls from that html content ? I tried lot of ways but can’t figure out how do I get.

Please provide someone some good solution for that.

Thanks in advance.

3

Answers


  1. Try using .each() to iterate img , link, script elements ; retrieve src or href attribute values

    $("img, link, script").each(function() {
      // do stuff with `this.src` or `this.href`
      console.log(this.src || this.href)
    })
    
    Login or Signup to reply.
  2.   $('a, img, link, script').each(function () {
            console.info($(this).attr('href'));
            console.info($(this).attr('src'));
        });
    
    Login or Signup to reply.
  3. If you want to get all “URLs” with pure JavaScript.

    HTML:

    <a href="www.google.com">1</a>
    <br>
    <a href="www.facebook.com">2</a>
    <br>
    <a href="www.yahoo.com">3</a>
    <br>
    <a href="www.bing.com">4</a>
    <br>
    <a href="www.youtube.com">5</a>
    <br>
    <a href="www.iRanOutOfNames.com">6</a>
    <br>
    

    JavaScript:

    function getURLs(url) {
      //create an empty array
      var array = [];
      // get all <a> tags
      //note: you can do that with <img/> tags or any
      //I only used the <a> tag for the sake of time
      url = document.getElementsByTagName("a");
      //loop through all of the elements
      for (var i = 0; i < url.length; i++) {
        //when done, add all the elements inside the empty array
        array.push(url[i].href);
      }
      //alert them
      alert(array);
    }
    //call the function
    getURLs();
    

    Demo: jsfiddle

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search