0

I'm sorry maybe my question is confusing to anyone..Actually I am not getting a right way to say is the following code is an javascript code or it is a json..

see this sample code which I made and it is working perfectly:

var txt = '{ "title" : "Asian Dad: B Again!? (you die)", "image" : "http://i.ytimg.com/vi/IN7o2Iy89WQ/default.jpg", "length" : "2", "status" : "serving", "progress_speed" : "", "progress" : "", "ads" : "", "pf" : "", "h" : "a80b4ceb1ad73a0222ca7a78dc448213" }';

var obj = eval ("(" + + ")");

document.getElementById("fname").innerHTML=obj.title 
document.getElementById("lname").innerHTML=obj.h 

This code is printing the value of the title & h.

But my actual problem is the code which is given inside var txt = "....." ! Is actually coming from an external website ...I'm posting that url here :

http://www.youtube-mp3.org/a/itemInfo/?video_id=IN7o2Iy89WQ

You can see it is little bit strange..

it seems like a json which but it is also contains executable javascript code...

Hope you'll notice that..

My problem is I want to get the var txt = '......' value from that external url whatever logic I tried got failed. nothing is working. I'm new with this executable javascript code and not able to find a way to load it thru that external url..

Can anyone help me to know how to parse that .title & .h data directly thru the url ?

Please any help or suggestion will be appreciated..

EDITED :

Friends, I am not getting a way to connect that external url in my script for getting the value of obj.title ...Do you got me ? the present script is using a sample code inside var txt = '{....}'; I want to replace it with the url http://www.youtube-mp3.org/a/itemInfo/?video_id=IN7o2Iy89WQ because I want to get obj.title value from that external url. it is a part of my program.

Hope that will help.

Lain
  • 2,166
  • 4
  • 23
  • 47
Kapil
  • 115
  • 3
  • 17
  • where is the executable code? – Nicolas Straub Sep 28 '13 at 12:16
  • *"how to purse executable javascript like json on webpage"* [JSON](http://json.org) isn't JavaScript, nor is it executable. – T.J. Crowder Sep 28 '13 at 12:16
  • @NicolásStraubValdivieso follow this url http://www.youtube-mp3.org/a/itemInfo/?video_id=IN7o2Iy89WQ – Kapil Sep 28 '13 at 12:25
  • @T.J.Crowder please follow this url http://www.youtube-mp3.org/a/itemInfo/?video_id=IN7o2Iy89WQ i want to parse this data in my webpage now what you think is it json or javascript or both... – Kapil Sep 28 '13 at 12:26
  • nothing in that code is executable. it's a plain old json string that defines an object with two properties. if you're having trouble getting the text because of cross domain issues, use jsonp to make the ajax request (http://stackoverflow.com/questions/6849802/jquery-getjson-works-locally-but-not-cross-domain will point you in the right direction) – Nicolas Straub Sep 28 '13 at 12:30
  • @NicolásStraubValdivieso i do not have cross domain issue. i am not getting a way to connect that external url in my script for getting the value of obj.title ...Do you got me ? the present script is using a sample code inside var txt = '{....}'; i want to replace it with the url http://www.youtube-mp3.org/a/itemInfo/?video_id=IN7o2Iy89WQ because i want to get obj.title from that external url – Kapil Sep 28 '13 at 13:03
  • 1
    @Kapil that is cross site scripting – Nicolas Straub Sep 28 '13 at 13:14

2 Answers2

1

If you trust the http://www.youtube-mp3.org website, you can just fetch that data with dynamically created <script> tag, and write a function which checks if the data are already available.

var script = document.createElement('script');
script.src = 'http://www.youtube-mp3.org/a/itemInfo/?video_id=IN7o2Iy89WQ';
document.body.appendChild(script);

var dataReady = function() {
   if (window.info)   {
       document.getElementById("fname").innerHTML=info.title; 
       document.getElementById("lname").innerHTML=info.h; 
   } else {
       //script haven't loaded yet, try again in half a second
       setTimeout(dataReady, 500);
   }
}
dataReady();

I am checking window.info, and not window.txt. That Is because the url you provided returns result in the form below:

info = { "title" : "Asian Dad: B  Again!? (you die)", "image" : "http://i.ytimg.com/vi/IN7o2Iy89WQ/default.jpg", "length" : "2", "status" : "serving",  "progress_speed" : "",  "progress" : "",  "ads" : "",  "pf" : "",  "h" : "154d6f3b7ac53fa2bb9f7cc5011d1c5d"  };

So, there is info variable instead txt.

Once again, you have to trust youtube-mp3.org to do that, and the result should have pretty stable format, or you can end up constantly checking if they have changed anything.

SWilk
  • 3,261
  • 8
  • 30
  • 51
  • Hi,,Nothing is showing....did you checked your script ? is showing anything..I am getting undefined .. – Kapil Sep 28 '13 at 19:44
  • I have written it here in SO, have not tested it. I might have misspelled something. You have not posted your html, so check if your fields have set `id` attributes to fname an lname, or correct the script. – SWilk Sep 30 '13 at 10:43
  • it is showing some details like status etc but most of details are not showing like title, h, etc...I do not know why..in the title value it is showing undefined and in the h value it is showing - ....but if i change title or h to status it showing data serving..i do not know whats wrong. – Kapil Sep 30 '13 at 11:46
  • did you check what exactly is returned by that service? What does console.log(info) show on chrome developer tools console? – SWilk Sep 30 '13 at 12:04
  • You know what ...when i test that code on my pc..it is working perfectly but when i am testing it on my server..it is not working..i think something youtube-mp3.org is stopping my program to fetch data from it online..that's the matter. – Kapil Sep 30 '13 at 12:31
  • Please read about SameOriginPolicy, maybe that will help you understand why it is not working. – SWilk Sep 30 '13 at 13:33
0
var txt = '{ "title" : "Asian Dad: B Again!? (you die)", "image" : "http://i.ytimg.com/vi/IN7o2Iy89WQ/default.jpg", "length" : "2", "status" : "serving", "progress_speed" : "", "progress" : "", "ads" : "", "pf" : "", "h" : "a80b4ceb1ad73a0222ca7a78dc448213" }';

var obj = JSON.parse(txt);


document.getElementById("fname").innerHTML=obj.title 
document.getElementById("lname").innerHTML=obj.h 

the snippet contains no executable code and this works fine (jsfiddle here)

Nicolas Straub
  • 3,381
  • 6
  • 21
  • 42