Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 views
in Technique[技术] by (71.8m points)

jquery - get url path?

I know I can use window.location.pathname to return a url, but how do I parse the url?

I have a url like this: http://localhost/messages/mine/9889 and I'm trying to check to see if "mine" exists in that url?

So, if "mine" is the second piece in that url, I want to write an if statement based on that...

if(second argument == 'mine') { do something }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
if ( location.pathname.split("/")[2] == "mine" ) { do something }

Although it would obviously be better to check whether there are enough items in the array that's returned by split:

var a = location.pathname.split("/");
if ( a.length > 2 && a[2] == "mine" ) { do something }

Note that even though array indexes are zero based, we want to specify 2 as the index to get what you refer to as the 2nd argument as split splits "/messages/mine/9889" into an array of 4 items:

["", "messages", "mine", "9889"]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...