Javascript Url object

Posted on 2009-04-24
Last Modified: 2012-05-06
Is there anything in JS that corresponds to java's object?
The location object does the work but i would like to create "my own" location object and not that of window.location. And you can't do "var url = new Location("")" can you?
I'm interested in the location methods .port, .host, path etc and would like a JS api to get the different parts of an url similar to java's URL. See java code ex below... url = new"");

String host = url.getHost(); // returns ""

int port = url.getPort(); // returns 80

String query = url.getQuery(); // return param=value

String path = url.getPath(); // returns /path

.. etc etc

Open in new window

Question by:vreten
    LVL 16

    Expert Comment

    I dont think you have anything equivalent of in javascript. You will have to get the location string using window.location.href and then get the required values by manipulating the string
    LVL 2

    Expert Comment

    location object is all you have in javascript for url manipulation.
    Url and location changes in javascript are a simple String.

    Author Comment

    The purpose is not to change url or location, that I know how to do. But I get your points. Simply have to write my own url_manipulation_api. But it would be nice to have simple functions like getHost(url), getPath(url), getProtocol(url) etc that not only returns the values but also make sure u have w3c valid url's.
    LVL 14

    Accepted Solution

    Hi, you might want to check out these articles regarding custom classes, methods and js.prototype:
    function myURL(url){
    //Create custom method
    function getHost(){
    function getPort(){
    function getQuery(){
    function getPath(){

    Open in new window


    Featured Post

    Course: HTML5 Specialist

    HTML5 development skills are critical to all developers. HTML5 is the foundation to almost any development process. That's why business, design studios, development shops and other organizations need HTML5 developers. Get your foot in the door as a HTML5 specialist.

    Join & Write a Comment

    I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now