return to homepage

Scripts: Javascript

Extract the filename from a URL

This script allows you to extract various elements of a filename from the url - this is usefull as it allows you to use the URL as a kind of cookie (i.e. you can store data their that defines the page - see my photo gallery for an example of this).

 

 


<SCRIPT Language=JavaScript>
<!--
// Extract whole URL
function extractWholeURL() {
var wholeURL=window.location.href;
}

// Extract whole filename from URL
function extractWholeFilename() {
wholeurl = window.location.href;
x = wholeurl.length;
while((wholeurl.substring(x,x-1)) != "/"){ x--; } clipstart = x;
var fileWholeName=wholeurl.substring(wholeurl.length,clipstart);
}

// Extract the filename without the extension
function extractJustFilename() {
wholeurl = window.location.href;
x = wholeurl.length;
while((wholeurl.substring(x,x-1)) != "."){ x--; } clipend = x;
while((wholeurl.substring(x,x-1)) != "/"){ x--; } clipstart = x;
var fileNameOnly=wholeurl.substring(clipend-1,clipstart);
}

// Extract the extension from the filename
function extractExtention() {
wholeurl = window.location.href;
x = wholeurl.length;
while((wholeurl.substring(x,x-1)) != "."){ x--; } clipstart = x;
var fileExtension=wholeurl.substring(wholeurl.length,clipstart);
}

//-->
</SCRIPT>