Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6894601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:48:11+00:00 2026-05-27T06:48:11+00:00

I am trying to make a program that grabs all the links from an

  • 0

I am trying to make a program that grabs all the links from an external website and display them using jQuery and PHP. Here are my steps:

  1. Get the html of a page using php (load.php)
  2. Put that html into a div
  3. Get all elements in that div

Here is my code (index.html):

<html>
<head>
    <title>Test</title>
    <script type="text/javascript" src="jquery.js">//jquery</script>
    <script type="text/javascript">
        $(function() { //on load
            var url = "http://google.com";
            $.post('load.php', { url: url},
                function(html) {
                    $('#page').html(html); //loads html from the page into a div

                    var links = $('#page > a');
                    alert('links.length: ' + links.length); //PROBLEM: returns 0 
                    for(var i=0; i < links.length; i++)
                    {
                        alert(links[i]);
                    }
            });
        });
    </script>
</head>
<body>
<div id="page" style=""></div>
</body>
</html>

And the php code (load.php):

<?php
$url = $_POST['url'];
$html = file_get_contents($url);
echo $html;
?>

The page is being loaded into the div correctly, so I know it is grabbing the html, but links.length is returning 0. So it is something wrong with this line:

var links = $('#page > a');

However, when I try to load it on my test page with html:

<a href="http://google.com">link1</a>
<a href="http://yahoo.com">link2</a>

links.length returns 2. Why does it work with my test page and not google?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:48:11+00:00Added an answer on May 27, 2026 at 6:48 am

    probably because your test page contains a document fragment (only the 2 links) while a page like google contains a whole document (starting with a doctype declaration and <html> and so on…).

    inserting such html into a div element probably breaks your DOM.

    I’d advise to

    1. parse the HTML serverside and pass only the results to your JS app.
      OR
    2. load the page (from your server) in an iframe and access it’s document to get to its link collection (documentOfIframe.links)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a program that Encrypts data using AES, then encrypts
I am trying to make a program that dynamically creates a button using the
I'm trying to make a program that will draw lines over a picturebox using
I'm trying to make a program that fetches information from this webpage, www.sio.no ,
I'm trying to make a program that goes through all squares of a chessboard
I am trying to make a program that shows all the active applications. I
I am trying to make a program that automatically corrects the perspective of a
I'm trying to make a program that reads the value at a certain address.
Hey, I am trying to make a program that minimises any program to the
I'm trying to make a small program that could intercept the open process of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.