Tuesday, April 10, 2007

javascritp to get application context path

Recently one of my project I required to get context root in js file .We can’t use request.getContextPath() in js file us I wrote javascript to get context root

function getContextRoot()
{
//Location object's href gives the complete URL
var url = window.location.href;
//We need to extract the context root from the full URL.
var regExp = /^http:\/\/[0-9a-zA-Z.]*\/[0-9a-zA-Z]*\//
var matches = url.match(regExp);

//var url = "http://172.28.49.18:8084/Cyclops/login.do
//it will return http://172.28.49.18:8084/Cyclops/
return matches ;
}

No comments: