问题
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