Results 1 to 6 of 6

Thread: How To Get URL Parts in JavaScript?

  1. #1
    Join Date
    Jul 2006
    Posts
    289

    How To Get URL Parts in JavaScript?

    Hi ..!! I am doing my project work in JavaScript.!! I that I want to obtain the URL from the window.location object. I have tried many different ways but I am not getting the appropriate results. Does anyone out there know how to get URL parts in JavaScript..?? Also if possible tell me the method to change the current location with window.location.href and window.location.reload. Hope that I am clear with my points..!! Please help me soon..!!
    Signatures reduce available bandwidth

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How To Get URL Parts in JavaScript?

    The location object is all that you have to use. For storing the URLs this class of JavaScript is used. By using this class you can get the URL parts in JavaScript. The location object class comes with properties that represent each part of the URL, and can be updated by changing the href property. The important things that you will have to check is :
    1. location.href : the full URL
    2. location.protocol, location.host & location.pathname : the parts of the URL

    Also the location object class has the following methods :
    • location.reload
    • location.replace

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How To Get URL Parts in JavaScript?

    You will have to follow the below steps for getting the URL parts in JavaScript :
    1. Before getting the URL parts, you should know the parts of URL. The URL consists of protocol, host and pathname. It looks like protocol://host/pathname.
    2. Then you will have to use the windows.location function to separate the parts out. For example, if you want to show an alert in your URL, you will have to do like the following code :
      Code:
      alert(window.location.protocol + "://" + window.location.host + "/" + window.location.pathname);
    3. Lastly you can split the pathname up by using the window.location.pathname.split() function, using the slash ("/") as a delimiter.

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: How To Get URL Parts in JavaScript?

    You can also put an array into the JavaScript program. You can also access the different parts by the parts of the array. You can do this by using the following code :
    Code:
    var secondLevelLocation = pathArray[0];
    And if you want to put that pathname back together, you can do that by pasting together the array and put the “/”’s back in. Just look at the following code so you can understand it clearly :
    Code:
    var newPathname = "";
    for ( i = 0; i pathArray.length; i++ ) {
      newPathname += "/";
      newPathname += pathArray[i];
    }

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How To Get URL Parts in JavaScript?

    You said that you want to get the different pieces of the url of the webpage. For doing this you would have to use the window.location object along with the JavaScript Split function. If you want to get the entire URL, you will have to put the following script :
    Code:
    <script type="text/javascript">
    
    completeURL= window.location;
    document.write(completeURL);
    
    </script>

  6. #6
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How To Get URL Parts in JavaScript?

    The script mentioned by the 'kelfro' is correct if you want to entire URL. You have also asked for the parts of URL. For getting the current URL in the parts you requested you can use the following coding :
    Code:
    <script type="text/javascript">
    
    fullDomain = window.location.host;
    
    domainArray = fullDomain.split('.');
    
    subDomain=domainArray[0];
    document.write(subDomain);
    
    domain=domainArray[1];
    document.write(domain);
    
    domainExt=domainArray[2];
    document.write(domainExt);
    
    urlPath=window.location.pathname;
    document.write(urlPath);
    
    </script>

Similar Threads

  1. Feedback on new parts?
    By hidden1 in forum Hardware Peripherals
    Replies: 1
    Last Post: 03-02-2010, 02:07 PM
  2. Bad parts of using IIS
    By Allen young in forum Software Development
    Replies: 3
    Last Post: 23-06-2009, 01:12 PM
  3. PC parts suggestion!
    By XDude in forum Hardware Peripherals
    Replies: 4
    Last Post: 30-12-2008, 09:21 PM
  4. Splitting a file .Mkv into two parts?
    By Abraham.J in forum Monitor & Video Cards
    Replies: 3
    Last Post: 23-12-2008, 07:29 PM
  5. Transformer from used car parts
    By RedZot in forum Off Topic Chat
    Replies: 4
    Last Post: 24-05-2008, 03:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,094,220.45976 seconds with 16 queries