How can I get “h” value from the link using javascript? [closed]

会有一股神秘感。 提交于 2019-12-16 18:07:10

问题


How can I get "h" value from the url using javascript? (ajax,jquery or etc.)

URL: http://www.youtube-mp3.org/a/itemInfo/?video_id=p8-pP4VboBk

URL Content:

info = { "title" : "Laura Branigan - Self Control", "image" : "http://i.ytimg.com/vi/p8-pP4VboBk/default.jpg", "length" : "5", "status" : "serving", "progress_speed" : "", "progress" : "", "ads" : "", "pf" : "http://ping.aclst.com/ping.php/2452159/p8-pP4VboBk?h=761944", "h" : "83135b0b3cf927b5e6caf1cf991b66b3" };

Purpose is getting and print this value (h) from the external URL;

83135b0b3cf927b5e6caf1cf991b66b3


回答1:


You can try this. I think it is simpler

info.h



回答2:


The string you have above is a complete JavaScript object, You can think of this as a hash table or an associative key array where "key":"value" if you want to index into this array or object you simply use it key thus Object['key'] where key can be an int or a string or any object




回答3:


Json objects are perfectly integrated in javascript.

This means that for a definition:

var myObject = { "name": "Doe", "parents": {"father": "Louis", "mother": "Ophelia"}};

You can simply access the data with following statements:

var myName = myObject.name;
var myFather = myObject.parents.father;
var myMother = myObject.parents.mother;

It really is as simple as that.

As an exact answer to your question, it is indeed info.h.

UPDATE: If you mean the h from the info.pf url, that would be

var h = info.pf.substr(info.pf.indexOf("?h=")+3, 999);



回答4:


Oh! Now I understand your question. You don't know how to get the response from a url into a javascript variable. You need a small ajax script for this:

var youTubeUrl = "http://www.youtube-mp3.org/a/itemInfo/?video_id=p8-pP4VboBk";

var request = makeHttpObject();
request.open("GET", youTubeUrl, true);
request.send(null);
request.onreadystatechange = function() {
  if (request.readyState == 4){
    eval(request.responseText);//this should create the info variable.
    alert(info.h); //<<<---this should be it!
    //TODO: add your code to handle the info.h here.
    }
};

function makeHttpObject() {
  try {return new XMLHttpRequest();}
  catch (error) {}
  try {return new ActiveXObject("Msxml2.XMLHTTP");}
  catch (error) {}
  try {return new ActiveXObject("Microsoft.XMLHTTP");}
  catch (error) {}

  throw new Error("Could not create HTTP request object.");
}

Code mostly copied from: https://stackoverflow.com/a/6375580/1311434

Note that I haven't tested this code, but I hope it get's you in the right direction. Be sure that you can trust the code you retrieve from the original URL as it's a bit dangerous to eval() code you retrieve from another site.



来源:https://stackoverflow.com/questions/17126303/how-can-i-get-h-value-from-the-link-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!