Terry Chen
Terry Chen

Reputation: 437

Load DOM content

Is there a way I can use Javascript to load a site's UI content in DOM? For example

<code>      
  <h1>Google</h1>
</code>

Retrieving "Google" (which has a h1 tag) in my Javascript code.

Upvotes: 0

Views: 77

Answers (1)

Jens A. Koch
Jens A. Koch

Reputation: 41796

Access DOM on your own page

To answer your exact question:

var h1 = $('h1').html();

Yes, you might add jQuery to your site and use it's selector, like so:

$( "#foo" )[ 0 ]; // = document.getElementById( "foo" )

Access DOM of other page

This is not so easy, because of Cross Domain Policies. Anyway, you will find out, if it is blocked. Try an Ajax query to get the website content, like so:

$(document).ready(function() {
baseUrl = "http://www.somedomain.com/";
$.ajax({
    url: baseUrl,
    type: "get",
    dataType: "",
    success: function(data) {
        // play with data
});

Upvotes: 1

Related Questions