Click here to Skip to main content
Click here to Skip to main content

Get WebSite Root Relative Path by JavaScript

By , 10 May 2013
 

Introduction

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.

Background

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;
}

License

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

About the Author

AkramOnly
Software Developer (Senior) Intergraph KSA
Egypt Egypt
Member
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.

Comments and Discussions

Comment 0 messages have been posted for this article Visit http://www.codeproject.com/Tips/591352/Get-WebSite-Root-Relative-Path-by-JavaScript to post and view comments on this article, or click here to get a print view with messages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130513.1 | Last Updated 11 May 2013
Article Copyright 2013 by AkramOnly
Everything else Copyright © CodeProject, 1999-2013
Terms of Use