0

I want to GET html from my second site(subdomain) and use it to build a part of this site.

HTML site:

<head>
<body>

<div id = "items">
  <div id = "#one">
    <ul>
       <li><a href = "#">Click here</a></li>
       <li><img src = "/images.png" /></li>
    </ul>
  </div>

  <div id = "#two">
    <ul>
       <li><a href = "#">Click here</a></li>
       <li><img src = "/images2.png" /></li>
    </ul>
  </div>
 </div>

</body>
</head>

I want to do things like counting number of div elements, getting list etc.

This is what I tried:

$.ajax({ 
 url: '/mysite/items.html', 
success: function(data) { 
  //Need to manipulate here

  var html = $(data);

  var items = $.get(".items", html);
  var numItems = $('.items').length;

  console.log(data); //There is data(Whole HTML is here.)

  console.log(items); //This is undefined??
  console.log(numItems): //This is undefined??

  var raw = html.get(".offers"); //Complains doesnot have a get method??

},
});

2 Answers 2

2

From what I can see what you want os

$.ajax({
    url: '/mysite/items.html',
    success: function (data) {
        //Need to manipulate here

        var $html = $(data);

        //use .find() to find elements with class items in data
        var $items = $html.find('.items');
        //use the items object to get the length
        var numItems = $items.length;

        console.log(data);

        console.log($items);
        console.log(numItems):

        //need to use .find() to get the elements matching a selector
        var raw = $htm.find(".offers");

    },
});
Sign up to request clarification or add additional context in comments.

Comments

0

Javascript can't load a non-javascript file from another domain because of the same-origin security policy. If you own both sites you can enable CORS

Otherwise You'll need a server-side language to act as a proxy

4 Comments

Hi I am loading this file from my subdomain. I can get HTML file just fine. The problem is with manipulating, getting elements and breaking them. Thanks
Javascript has no default way of parsing DOM documents like xml/html. You'll need to find a library for it, like jquery
@helion3 he is using jQuery at the moment it seems.
I see, he added the jquery code to the question. Nice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.