0

I have a string like this

http://www.example.com/xyz/filename.php

Now I just want to get the string http://www.example.com/ how can I do this ?

I know this is a simple question but I have tried its not working

Thanks in Advance

Vikram Anand Bhushan
  • 4,836
  • 15
  • 68
  • 130
  • 1
    What have you tried and what did not work? Did you check that link: http://stackoverflow.com/questions/2597049/strip-domain-name-from-url-string – John Sep 02 '14 at 12:23
  • 1
    duplicate --- http://stackoverflow.com/questions/4815559/how-to-get-domain-name-from-url-using-jquery – Bob Tate Sep 02 '14 at 12:24
  • Webkit-based browsers and Firefox as of version 21 know window.location.origin. To target all browsers, I use the following **if (!window.location.origin) window.location.origin = window.location.protocol+"//"+window.location.host;** – Dharma Sep 02 '14 at 12:36

1 Answers1

2

Try this:

var $url = 'http://www.example.com/css/filename.php';

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

var $finalUrl = a.protocol + '//' + a.hostname + '/';
Suganth G
  • 5,136
  • 3
  • 25
  • 44