-3
$url = 'http://hostname.com/folder/folder2/test.php?id=243432432424243';

from the variable above can i have like as follow:

$finalUrl = 'http://hostname.com';

if yes than please answer this.

user2640758
  • 73
  • 1
  • 3
  • 11
  • DUPLICATE..Duplicate everywhere... -> [http://stackoverflow.com/questions/4815559/how-to-get-domain-name-from-url-using-jquery](http://stackoverflow.com/questions/4815559/how-to-get-domain-name-from-url-using-jquery) – Vikas Arora Dec 10 '13 at 12:26

2 Answers2

1
function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));
    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);
        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}
1
var $url = 'http://hostname.com/folder/folder2/test.php?id=243432432424243';

var a = document.createElement('a');
a.href = $url;

var $finalUrl = a.protocol + '//' + a.hostname;
adeneo
  • 312,895
  • 29
  • 395
  • 388