te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>c# - How to get route parameter in JavaScript? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to get route parameter in JavaScript? - Stack Overflow

programmeradmin4浏览0评论

I have a ASP.NET website that is URL-Rewriting enabled. I know how to get route parameters using C#:

Page.RouteData.Values["id"] as string;

But I don't know how to get it from javascript? Following is my rewritten link format:

/{id}/{title}

I want to get this {id} field using JavaScript. Please help!

UPDATE

I have this code that gets request parameter.

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

How can this be modified to get route parameter?

I have a ASP.NET website that is URL-Rewriting enabled. I know how to get route parameters using C#:

Page.RouteData.Values["id"] as string;

But I don't know how to get it from javascript? Following is my rewritten link format:

http://www.domain./topic/{id}/{title}

I want to get this {id} field using JavaScript. Please help!

UPDATE

I have this code that gets request parameter.

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

How can this be modified to get route parameter?

Share Improve this question asked Nov 30, 2013 at 16:40 Aishwarya ShivaAishwarya Shiva 3,40616 gold badges61 silver badges111 bronze badges 5
  • Check this post, I think you'll find the solution there: link – jalba Commented Nov 30, 2013 at 16:43
  • URL-parameter is different thing. I don't think it will work. – Aishwarya Shiva Commented Nov 30, 2013 at 16:44
  • if your format is http://www.domain./topic/{id}/{title} then why don't you window.location.href in a string; split it on '/'; and then access the particular index. – Vikram Deshmukh Commented Nov 30, 2013 at 16:49
  • @srvikram13 please provide an example what you exactly want to say. – Aishwarya Shiva Commented Nov 30, 2013 at 16:55
  • @AishwaryaShiva check the solution I've posted below. – Vikram Deshmukh Commented Nov 30, 2013 at 17:25
Add a ment  | 

2 Answers 2

Reset to default 8
function getValueAtIndex(index){
    var str = "http://www.sample./234/Fiddle/test"; //window.location.href; 
    return str.split("/")[index];
}
console.log(getValueAtIndex(3));

Update

Here's a much cleaner way of getting value at an index in the route using plain JS:

    location.pathname.split('/')[index]
    

For example, for the current page,

location.pathname = '/questions/20302776/how-to-get-route-parameter'

You can get the question id by doing:

location.pathname.split('/')[2]

// Notice that the index does not start from zero.

Try the following:

const getRouteValueAt = (idx) => {

   const routeTokens = location.pathname.replace(/^\/+/g,'').split('/')
   //[questions,20302776,how-to-get-route-parameter-in-javascript]

   return routeTokens.length > idx ? routeTokens[idx] : undefined
}

Usage

const routeName = getRouteValueAt(0); // questions
const routeId = getRouteValueAt(1); // 20302776
发布评论

评论列表(0)

  1. 暂无评论