views:

13

answers:

1

Hello

I want to get the current URL using javascript/jquery. The URL should include subfolders/subsites in the path but not the page or any other query parameters.

Examples

http://test.com/site1/folder/abc/test.aspx

Output : http://test.com/site1/folder/abc/


http://test.com/site1/folder/abc/test.aspx?id=1&name=xyz

Output : http://test.com/site1/folder/abc/


http://test.com/site1/folder/abc/test.aspx?id=1,name=xyz

Output : http://test.com/site1/folder/abc/


http://test.com/site1/folder/abc/?testid=45

Output : http://test.com/site1/folder/abc/

Any ideas?

+3  A: 

demo

just javascript will do...

var str = "http://test.com/site1/folder/abc/?testid=45";    
str.slice(0,str.lastIndexOf('/')+1); // prints http://test.com/site1/folder/abc/

to get the current URL,

var str = window.location.href;    
str.slice(0,str.lastIndexOf('/')+1);
Reigel
The last occurrence is not a good idea. Image a URL like `http://example.com/#/foo`.
Gumbo