Get The URL In JavaScript

If you need to get the URL for a page, let's say, so you can parse it. The easy way is to use window.location. A friend of mine stumbled upon a problem with this. He was using the URL as the origin for all of his AJAX calls. Originally he just used window.location.origin but this is not supported in Opera.

    var g_origin = window.location.origin;
    if ( ! g_origin ) {         // Probablyl running on Opera
        g_origin = window.location.protocol + "//" +;

The window.location also has the pathname for the URL. This is

    var g_pathname = window.location.pathname;

and it works in Opera as well as the other browsers.

•       •       •       •       •       •

Summary: # of Words: 183
Author: Philip Schlump
Published On: 2013-05-05

Download code from this articles in .tar.gz for Mac/Linux/Unix or .zip with CR/LF for Windows format.


Before You Go....

Have you read "Unintend Consinsequences"?

"I laughed so hard it hurt..."
    Rod Brown

"Incredibly funny! Incredibly true!"
    Tad Stevens