This is a bit of a hack, but you are client side so :)
This code makes some assumptions - like that the script tage for myscript.js
exists
Assume you have some markup on your page like so:\
<script src='../js/myscript.js' type="text/javascript"></script>
<div>hiya</div>
OK, now we will use the script tag and get the base url from that :)
Let's create a Util class we can use, add a get base url method and alert that value:
Working copy here: http://jsfiddle.net/SmCLy/
function Util() { /*...Nothing...*/
}
Util.getBaseURL = function() {
//<summary>
// Returns the application base URL (well, a URL that is
// equivalent to such - it may have some "backtracking",
// i.e. "../" at
// the end of the URL to simulate the root...)
//</summary>
// 1) Find the script include for this JavaScript file:
var scriptElements = document.getElementsByTagName("script"),
getScriptPathFragmentPosition = Util.getBaseURL._getScriptPathFragmentPosition,
getScriptPathFragmentPositionResult = getScriptPathFragmentPosition(scriptElements, "js/myscript.js"),
posScriptPathFragment = getScriptPathFragmentPositionResult.posScriptPathFragment;
// 2) Create the "base" URL by taking the current URL,
// stripping the page from the end, and appending
// sufficient "../" sequences to
// construct the equivalent of the application's root URL:
var scriptElementSrcLower = getScriptPathFragmentPositionResult.scriptElementSrcLower;
var backPathToRoot = ((scriptElementSrcLower !== "") ? scriptElementSrcLower.substring(0, posScriptPathFragment) : "");
var currentPath = location.href;
var currentPathWithoutPage;
var PAGE_TOKEN = ".aspx";
var posPageToken = currentPath.toLowerCase().indexOf(PAGE_TOKEN);
if (posPageToken > -1) {
var trimmedPath = currentPath.substring(0, posPageToken + PAGE_TOKEN.length);
currentPathWithoutPage = trimmedPath.substring(0, trimmedPath.lastIndexOf("/") + 1);
} else {
currentPathWithoutPage = currentPath.substring(0, currentPath.lastIndexOf("/") + 1);
}
return (currentPathWithoutPage + backPathToRoot);
};
Util.getBaseURL._getScriptPathFragmentPosition = function(scriptElements, scriptPathFragment) {
var scriptElementsIndex = (scriptElements.length - 1),
scriptElementSrc = "",
scriptElementSrcLower = "",
posScriptPathFragment = -1;
while (scriptElementsIndex >= 0) {
scriptElementSrc = scriptElements[scriptElementsIndex].getAttribute("src");
if (typeof(scriptElementSrc) != "undefined" && scriptElementSrc !== null && scriptElementSrc !== "") {
scriptElementSrcLower = scriptElementSrc.toLowerCase();
posScriptPathFragment = scriptElementSrcLower.indexOf(scriptPathFragment);
if (posScriptPathFragment >= 0) {
break;
}
}
scriptElementsIndex--;
}
var result = {
posScriptPathFragment: posScriptPathFragment,
scriptElementSrcLower: scriptElementSrcLower
};
return result;
};
alert(Util.getBaseURL());
SO, on your page you could do:
var myajaxUrl = getBaseURL()+"MyPage/GetDetails";
getDetails
i.e. without any extension?