Forum Moderators: open

Message Too Old, No Replies

Looking up page location in javascript?

         

Linda_A

5:54 pm on Apr 28, 2005 (gmt 0)

10+ Year Member



I have a PHP solution for determining which folder a particular page resides in and transforming that information into an ID which was meant to be used in a javascript to load the right submenu for that page.

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];

?>

gph

10:21 pm on Apr 28, 2005 (gmt 0)

10+ Year Member



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)))

Linda_A

10:46 pm on Apr 28, 2005 (gmt 0)

10+ Year Member



Which part of the PHP code would that replace? I'm afraid I don't understand what that bit of javascript would do. :)

gph

12:13 am on Apr 29, 2005 (gmt 0)

10+ Year Member



LOL, I don't understand the PHP!

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>

Linda_A

8:53 am on Apr 29, 2005 (gmt 0)

10+ Year Member



Ah, okay. I don't need the full path. :)

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?

BlobFisk

11:47 am on Apr 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Very 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

Linda_A

12:42 pm on Apr 29, 2005 (gmt 0)

10+ Year Member



Thanks. :) I'll see what I can do with this.

Bernard Marx

4:19 pm on Apr 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



// "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() )