jquery - get part of path?

ⅰ亾dé卋堺 提交于 2020-01-06 06:50:23

问题


I have a url that looks like this : http://mysite.com/1/2/Simpson and I want to create a variable that contains just Simpson...

Not sure how to do it here...

I have:

var myvar = window.location.pathname

and have tried something using substring, but can't seem to get it to work?


回答1:


this may work, but you might as well use the string window.location.href, not the object window.location (which contains many different objects within it):

var dirs = window.location.href.split( '/' );

however, there is a window.location.pathname that you could split, too. it just depends on if you want the part in front of the first "/" to be in the arrays as well (http://mysite.com).

var dirs = window.location.pathname.split( '/' );


来源:https://stackoverflow.com/questions/1931535/jquery-get-part-of-path

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