Javascript Url object

Posted on 2009-04-24
Medium Priority
Last Modified: 2012-05-06
Is there anything in JS that corresponds to java's java.net.URL 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("http://whatever.com")" 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...
java.net.URL url = new java.net.URL("http://www.domain.com/path/file.html?param=value");
String host = url.getHost(); // returns "www.domain.com"
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

ID: 24223442
I dont think you have anything equivalent of java.net.url in javascript. You will have to get the location string using window.location.href and then get the required values by manipulating the string

Expert Comment

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

Author Comment

ID: 24223662
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

sam2912 earned 2000 total points
ID: 24223927
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

621 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