Grab the youtube Video ID with Jquery & .match()

前端 未结 2 1056
梦谈多话
梦谈多话 2021-02-11 04:09

Just need a little push as I have this almost working.

I want to feed jquery a URL and have it strip everthing but the video url.

Here is my code:



        
2条回答
  •  广开言路
    2021-02-11 04:44

    if you are not forced to use match, you can use "split":

    var getList = function(url, gkey){
    
            var returned = null;
    
            if (url.indexOf("?") != -1){
    
              var list = url.split("?")[1].split("&"),
                      gets = [];
    
              for (var ind in list){
                var kv = list[ind].split("=");
                if (kv.length>0)
                    gets[kv[0]] = kv[1];
            }
    
            returned = gets;
    
            if (typeof gkey != "undefined")
                if (typeof gets[gkey] != "undefined")
                    returned = gets[gkey];
    
            }
    
            return returned;
    
    };
    
    var url = 'http://www.youtube.com/watch?v=bxp8NWvIeSo';
    $result = getList(url, "v");
    alert($result);
    

提交回复
热议问题