Forum Moderators: open
However, after getting that far, it turns out there doesn't seem to be a way to pass that info from PHP to javascript, so I am wondering if it would be possible to do it all in javascript?
This is the PHP code in question:
<?php $SubMenu = Array (
'/Citadel/' => None,
'/Citadel/FAQ' => QA,
'/Citadel/SSM' => QA,
'/Citadel/Concordance' => Theme,
'/Citadel/Encyclopaedia' => Theme,
'/Citadel/Heraldry' => Theme,
'/Citadel/History' => Theme,
'/Citadel/Characters' => Story,
'/Citadel/Prophecies' => Story,
'/Citadel/Artwork' => Miscellany,
'/Citadel/Books' => Miscellany,
'/Citadel/Images' => Miscellany,
'/Citadel/Links' => Miscellany
);
$SubMenuItem = Array (
'/Citadel/' => None,
'/Citadel/FAQ' => FAQ,
'/Citadel/SSM' => SSM,
'/Citadel/Concordance' => Concordance,
'/Citadel/Encyclopaedia' => Encyclopaedia,
'/Citadel/Heraldry' => Heraldry,
'/Citadel/History' => History,
'/Citadel/Characters' => Characters,
'/Citadel/Prophecies' => Prophecies,
'/Citadel/Artwork' => Artwork,
'/Citadel/Books' => Books,
'/Citadel/Images' => Images,
'/Citadel/Links' => Links
);
$URL = $_SERVER['PHP_SELF'];
$Folders = substr($URL,0,strrpos($URL,"/"));
$WhichSubMenu = $SubMenu[$Folders];
$WhichSubMenuItem = $SubMenuItem[$Folders];
?>
That js reads the location of the file and gets the full path to its folder. Try this test page and let me know if you need more help.
<html>
<head>
<title>test</title>
<script type="text/javascript">
String.prototype.folderOf=function(no_slash){
return this.slice(0,this.lastIndexOf('/',this.length-(/\/$/.test(this)?2:0))+(no_slash?0:1))
};
alert('with last slash = '+unescape(location.href.folderOf()))
alert('without last slash = '+unescape(location.href.folderOf(1)))
</script>
</head>
<body>
</body>
</html>
If possible, what I need is a maximum of two folders from root.
For example:
www.domain.com/Folder/somefile.html --> should return /Folder/
www.domain.com/Folder/Subfolder/somefile.html --> should return /Folder/Subfolder/
www.domain.com/Folder/Subfolder/SubfolderTwo/somefile.html --> should return /Folder/Subfolder/
Is that doable?
You need to use split(). This will create an array from the string you are splitting with each array item being the portions of the string between your split control.
For example:
var thePath="www.domain.com/Folder/Subfolder/somefile.html";
var pathArray=thePath.split("/");
You will then have an array with the strings between the forward slashes.
HTH
// "http://www.domain.com/folder/sub/[file.htm]"
// --> ['www.domain.com','folder','sub']
String.prototype.getParentFolderNames = function()
{
return this.replace( /^[^\/]+\//,'')
.replace(/\/[^\/]*$/,'')
.split(/[\/]/);
}function getPathUpTo2FoldersDeep()
{
//real use:// var fNames = location.href.getParentFolderNames().slice(1);
var fNames = web3p.getParentFolderNames().slice(1);
if(fNames.length > 2) fNames.length = 2 ;
return '/'+fNames.join('/')+'/';
}// test strings
web1 = 'http://www.domain.com/Folder/file.html';
web1p = 'http://www.domain.com/Folder/';
web2 = 'http://www.domain.com/Folder/Subfolder/file.html';
web2p = 'http://www.domain.com/Folder/Subfolder/';
web3 = 'http://www.domain.com/Folder/Subfolder/SubfolderTwo/file.html';
web3p = 'http://www.domain.com/Folder/Subfolder/SubfolderTwo/';alert( getPathUpTo2FoldersDeep() )