Splitting and Capturing jQuery URLs

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

Is there a place to grab the url and spit out the parts after #! / I want to edit and id.

any help is appreciated.

+4
source share
2 answers
var edit = window.location.hash.split('/')[1], ID = window.location.hash.split('/')[2]; 
+6
source

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 need a number after editing, you can also use regex

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

You can see how they work here: http://jsfiddle.net/jfriend00/4BTyH/

+10
source

Source: https://habr.com/ru/post/1398925/


All Articles