简体   繁体   中英

Extracting part of current page's URL in javascript

In ASP.NET MVC 3 site, I open the following uri

http://myserver/incidents/supercompany/register

That page needs to make use of ajax JSON retrieval mechanism with the help of jQuery:

$.getJSON('/Incidents/[[COMPANY NAME GOES HERE]]/SearchPeople/' + request.term, function (data) 

I am new to javascript. How can I obtain supercompany part of my current url to build the

/Incidents/supercompany/SearchPeople/ string?

Assuming your URLs follow a logical structure, you can do this

var URLparts = window.location.toString().substr(7).split('/');

And you can now access URLparts[1] to get the company name. Obviously, you need to be able to know that that will ALWAYS be there, but it's a solution if you can guarantee that (eg. with htaccess).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM