Click here to Skip to main content
12,952,792 members (34,465 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 10 May 2013

Get WebSite Root Relative Path by JavaScript

, 10 May 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Get root relative WebSite path to concatenate it with any HTML src path


In this tip, I will define a JavaScript function that finds the website name and its root Virtual Folder to be ready if we want to run an HTML page which resides on the root folder of the website.

The problem starts when we are in a webform or HTML page in a subfolder in the website and we don't know how many folders are above the containing folder to set the property src to the correct root path in the website. Now we have no need to use the '../' to move up one level since we exactly know the root path.


You need to know:

  • HTML
  • JavaScript

Using the Code

Just put the following JavaScript function which will return the full root path of a website also including the IIS Express port if found.

function getRootWebSitePath()
    var _location = document.location.toString();
    var applicationNameIndex = _location.indexOf('/', _location.indexOf('://') + 3);
    var applicationName = _location.substring(0, applicationNameIndex) + '/';
    var webFolderIndex = _location.indexOf('/', _
    location.indexOf(applicationName) + applicationName.length);
    var webFolderFullPath = _location.substring(0, webFolderIndex);

    return webFolderFullPath;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) Intergraph KSA
Egypt Egypt
I am a software developer since 2003. I am graduated from the faculty of business administration - IT Section at year 2001 (Cairo). I am specialized in tailored software and I love CodeProject very much.

You may also be interested in...

Comments and Discussions

Questioninstead of local host i need to display webapplication absolute path Pin
Member 1034214629-Dec-15 23:44
memberMember 1034214629-Dec-15 23:44 
QuestionImproved version: 2 lines of code Pin
Member 113536295-Jan-15 5:51
memberMember 113536295-Jan-15 5:51 
AnswerRe: Improved version: 2 lines of code Pin
Chris Carter5-Nov-16 6:56
memberChris Carter5-Nov-16 6:56 
GeneralMy vote of 1 Pin
Jeff Tian29-May-14 20:15
memberJeff Tian29-May-14 20:15 
GeneralMy vote of 5 Pin
davorko8313-Mar-14 5:44
memberdavorko8313-Mar-14 5:44 
GeneralMy vote of 5 Pin
Uffman18-Feb-14 3:20
memberUffman18-Feb-14 3:20 
GeneralMy vote of 5 Pin
JMK2028-Aug-13 7:03
memberJMK2028-Aug-13 7:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 11 May 2013
Article Copyright 2013 by AkramOnly
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid