我正在尝试解析页面的网址.例如,当前页面是location.href.
所以我的博客的主页,如果我使用alert(location.href); 它会在警告框中返回" http://diaryofthedead.co.cc/ ".如果我使用alert(location.href); 在我的博客的第二页,它会在一个警告框中返回" http://diaryofthedead.co.cc/page/2 ".有没有办法解析URL以获得最后的数字.有谁知道我怎么能这样做?我可以使用通配符或其他东西来做类似的事情:location.href +"page /"+*;*等于"page /"后面的任何内容,然后将*转换为变量?
Mar*_*odF 28
您可以使用
var pagenum = location.pathname.match(/\/page\/(.*)/)[1];
Run Code Online (Sandbox Code Playgroud)
它会在您的网址中提取超过'/ page /'的内容;
dou*_*osh 13
查看此库http://code.google.com/p/jsuri/
或者你想保持简单http://james.padolsey.com/javascript/parsing-urls-with-the-dom/
请查看位置对象http://www.w3schools.com/jsref/obj_location.asp上的文档
您首先需要"pathname"部分location.pathname
| 归档时间: |
|
| 查看次数: |
78286 次 |
| 最近记录: |