0

I have a url http://xyz.in/pqr/v/index.php and I want to get only http://xyz.in/pqr/v/ so how can I remove index.php from url.

7
  • 1
    possible duplicate of JS: Most optimized way to remove a filename from a path in a string? Commented Jan 2, 2014 at 7:35
  • 1
    url.split('/').pop(); Commented Jan 2, 2014 at 7:36
  • @adeneo No. He doesnt want index.php. he wants the rest. Commented Jan 2, 2014 at 7:38
  • @RoyiNamir then use what's left on the array: var arr = url.split('/); arr.pop(); arr = arr.join('/'); Commented Jan 2, 2014 at 7:41
  • @Kroltan dont tell it to me. Commented Jan 2, 2014 at 7:42

5 Answers 5

6

Try this.

  var host = window.location.protocol+'//'+window.location.host+'/'
  alert(host);

if you want to avoid '/' at last, You can use this

var host2 = window.location.protocol+'//'+window.location.host

SEE THIS FIDDLE DEMO

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

1 Comment

but how will you get /pqr/v/ paths as mentioned in the question?
3

This is the simplest way to get base url:

window.location.origin

Comments

2

you can use .replace() function

try with this FIDDLE

var getUrl  = "http://xyz.in/pqr/v/index.php";
var change = getUrl.replace('index.php',''); 

NOTE : If your 'index.php' name is static

Comments

0

try this

function GetBaseUrl() {
    try {
        var url = location.href;

        var start = url.indexOf('//');
        if (start < 0)
            start = 0 
        else 
            start = start + 2;

        var end = url.indexOf('/', start);
        if (end < 0) end = url.length - start;

        var baseURL = url.substring(start, end);
        return baseURL;
    }
    catch (arg) {
        return null;


  }
}

Comments

0

Try this

var a='http://xyz.in/pqr/v/index.php';
alert(a.substring(0,a.lastIndexOf("/"))); //http://xyz.in/pqr/v"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.