0

Is there a way to hyperlink bits of information from different Url's for example the amount of views on a Youtube video to my website using jQuery or Javascript?

enter image description here

I have tried:

<script>
$(document).ready(function(){
    $('#response').load('http://www.youtube.com/watch?v=lrC2alc_ekol .watch-view-count');
});
</script>

HTML:

<div id="response"></div>
maxisme
  • 3,974
  • 9
  • 47
  • 97

2 Answers2

2

Yes, this is called an application programming interface or API for short. If you're looking for the YouTube API you can find it here: https://developers.google.com/youtube

Jon Koops
  • 8,801
  • 6
  • 29
  • 51
0

Yes, i.e. You can use jQuery Load, and once you have load the external page, extract just what you want with a selector for the specific info.

jQuery load external site page

i.e.

$('#myEl').load('remotePageUrl #remotepageIDselector');

Where myEl is an element in your page, remotePageUrl is the external site, and remotepageIDselector is the id of the element that contains the info that you want, you can use any selector.

Update: @user2768038, I have to go man, but the following code snippet should work, what happens is that you are doing a cross domain call, the previous answer works for your own domain, but the following code should work:

$.getJSON('http://anyorigin.com/get?url=youtube.com/watch?v=lrC2alc_eko&callback=?', function(data){

    var valueToFind;
    $.each($(data.contents), function(i, el) {

        // if we have the value scape ffrom the cicle
        if(valueToFind && typeof valueToFind !== undefined) {
            // alert the requested value
            alert(valueToFind);
            return false;
        }

        // search for the element in the current dom section
        valueToFind = $(el).find('.watch-view-count').html();

    });
});

But this is really something: 1- Expensive you're doing a full request to a page, 2- Expensive you're traversing the full DOM tree multiple times. 3- Not safe because the external site owner can change the code anytime.

Also my solution is far away from optimum, I'll update the post later night, but this should work locally at least in Firefox, and crossbrowser once you publish your code to a server.

Community
  • 1
  • 1
Roberto Alarcon
  • 1,430
  • 2
  • 18
  • 32
  • How do you find a selector for the specific info? – maxisme Oct 19 '13 at 20:20
  • 1
    @GuillaumePoussel The question actually says Get data from a url to put on website - not specifically youtube. – maxisme Oct 19 '13 at 20:21
  • @GuillaumePoussel youtube was just an example, what if the developer wants to extract info from another site that does not have an api. – Roberto Alarcon Oct 19 '13 at 20:21
  • Right, but that's usually considered as a bad practice to extract data from an external website in such way. You increase bandwith usage of the target website... – Guillaume Poussel Oct 19 '13 at 20:25
  • 1
    @GuillaumePoussel Not going to arg that (it is a bad practice, even for you as the developer, your code will break when the remote site change their html structure), but the question was about something, I just post once alternative of many. This was not about best practices. – Roberto Alarcon Oct 19 '13 at 20:28
  • @rob.alarcon Why is this not working? I am just expecting a number http://jsfiddle.net/Kfv36/ – maxisme Oct 19 '13 at 20:45
  • @user2768038 because currently there's no cross-domain ajax calls in jsfiddle http://stackoverflow.com/questions/7374271/how-to-use-ajax-request-in-jsfiddle – Roberto Alarcon Oct 19 '13 at 20:51
  • it is not working normally either! Please see my edit of my question – maxisme Oct 19 '13 at 20:54