-1

大家好,我有一个网址,我需要从网址获取参数

   var URL="http://localhost:17775/Students/199/Kishore"
   //here from the url i need to get the value 199

这是我一直在尝试的,但这里的值为 null

  function getURLParameter(name) { 
    return parent.decodeURI((parent.RegExp(name + /([^\/]+)(?=\.\w+$)/).exec(parent.location.href) || [, null])[1]); 
  };

  $(document).ready(function() {
     getURLParameter("Students");
     //i need to get the value 199 from the url
  });
4

4 回答 4

2

尽管可以使用 jQuery,但它并不需要。有很多方法可以给这只猫剥皮。像这样的事情应该让你朝着正确的方向开始:

var URL="http://localhost:17775/Students/199/Kishore";
var splitURL = URL.split("/");
var studentValue = "";

for(var i = 0; i < splitURL.length; i++) {
    if(splitURL[i] == "Students") {
        studentValue = splitURL[i + 1];
        break;
    }
}

这是一个工作小提琴

编辑

根据评论,表明位置将始终相同,提取很简单:

var url = "http://localhost:17775/Students/199/Kishore";
var studentValue = url.split("/")[4];
于 2013-04-12T17:55:17.017 回答
0

这就是您要查找的内容,因为 URL 参数会不断变化:

http://jsbin.com/iliyut/2/

var URL="http://localhost:17775/Students/199/Kishore"
var number = getNumber('Students'); //199

var URL="http://localhost:17775/Teachers/234/Kumar"
var number = getNumber('Teachers'); //234

function getNumber(section) {
  var re = new RegExp(section + "\/(.*)\/","gi");
  var match = re.exec(URL);
  return match[1];
}
于 2013-04-12T18:21:58.527 回答
-1

我会做以下事情:

var url = "http://localhost:17775/Students/199/Kishore"; 
var studentValue = url.match('/Students/(\\d+)/')[1]; //199
于 2013-04-12T18:16:56.553 回答
-2

如果你想要的块总是在同一个地方,这将起作用

var url="http://localhost:17775/Students/199/Kishore"

//break url into parts with regexp
//removed pointless regexp
var url_parts = url.split('/'); 

//access the desired chunk
var yourChunk = url_parts[4]

console.log(yourChunk)
于 2013-04-12T18:05:42.337 回答