1

URL:

http://testwebsite.com/page1.aspx#page/1

I want to split the URL upto aspx using jQuery or Javascript, meant I want to split the URL so that it should be:

 http://testwebsite.com/page1.aspx

I have tried this code;

var url=document.URL;
var arr=url.split('#');
var myoutput=arr[0];

Is it the right way to split? Is there any other way which is reliable and better?

1
  • location.host + location.pathname.split('#')[1]; Commented Nov 25, 2013 at 10:50

3 Answers 3

6

You could just use location.host + location.pathname to generate the specific page path instead of trying to split down a string to contain only what you want it to.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 - but location.origin might be better than location.host, depending on usage.
location.origin returning http:// is that it is intended for? what is difference between host and origin?
4

Try:

var url=window.location.href;
var arr=url.split('#')[0];

Fiddle here.

Comments

0

If your intended goal is to grab the URL up to the point of the .aspx extension, I would suggest splitting the URL at the extension and then appending it to the split. This would account for other symbols that could possibly precede the extension:

var url = document.URL,
    arr = url.split('.aspx'),
    myOutput = arr[0] + '.aspx';

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.