Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery URL split and grab

Tags:

jquery

url

split

So I have a URL and I know how to get $_GET from URL however my URL is http://www.example.com/#!/edit/2695.

Is there away to grab the url and spit the parts after #!/? I want edit and the ID.

like image 513
RussellHarrower Avatar asked Feb 29 '12 00:02

RussellHarrower


2 Answers

You can use this code

var url = "http://www.mysite.com/#!/edit/2695";
var pieces = url.split("/#!/");
pieces = pieces[1].split("/");

// pieces[0] == "edit"
// pieces[1] == "2695"

If you just wanted the number after the edit, you could also use a regex

var url = "http://www.mysite.com/#!/edit/2695";
var match = url.match(/#!\/edit\/(\d+)/);
if (match) {
    // match[1] == "2695"
}

You can see both of them work here: http://jsfiddle.net/jfriend00/4BTyH/

like image 124
jfriend00 Avatar answered Oct 19 '22 15:10

jfriend00


var edit = window.location.hash.split('/')[1],
      ID = window.location.hash.split('/')[2];
like image 35
adeneo Avatar answered Oct 19 '22 16:10

adeneo