Archive

Archive for the ‘Javascript’ Category

Javascript string split

August 16, 2011 Leave a comment

Javascript string split or extact

<script type=”text/javascript”>

var mySplit = ‘1-56’;
var partsArray = mySplit.split(‘-‘);
alert(partsArray[0]);
alert(partsArray[1]);

</script>

 

 

Advertisements
Categories: Javascript

Javascript: Get Base Url

July 19, 2010 12 comments

In the code below, you will be able to find the baseUrl of your website using javascript.   The following javascript code will work when used on your localhost or when it’s used in a live site (finds the root url of the domain address).

Just add this javascript function to a .js file or script tag :

function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));


    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}

If you’d like to test the getBaseURL function in an html page and view the result, simply add the following  after the closing tag of the getBaseUrl() function:

document.write(getBaseURL());
Categories: Javascript