...

View Full Version : Need help with getting name of file WITHOUT extension...



ShadowIce
06-03-2009, 05:56 PM
Hi all, I've been doing some research on url names. The problem is, I need this code to return like say "index.php", it would return "index". But idk where I went wrong..

Anyway..

The code:


<script>
var sPage;
var sPath = window.location.pathname;
//var sPage = sPath.substring(sPath.lastIndexOf('\\') + 1);
//var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
if(sPath.substring(sPath.lastIndexOf('.') + 1)){
sPage=sPath.split("",3);
}
sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
alert(sPage);
</script>

ANY help is GREATLY appreciated! :)

~SI~

venegal
06-03-2009, 06:26 PM
Just use a regexp:

var sPath = window.location.pathname;
var sPage = sPath.match(/.*\/(.*?)\./)[1];
alert(sPage);

itsallkizza
06-03-2009, 06:27 PM
Here ya go:


function stripExtension(str)
{
var pieces = str.split(".");
if (pieces.length > 1) pieces.pop();
return pieces.join(".");
}

var filename = "index.php";
alert(stripExtension(filename));
filename = "my.index.php";
alert(stripExtension(filename));
filename = "http://www.example.com/my.index.php";
alert(stripExtension(filename));

Philip M
06-03-2009, 06:27 PM
var url = "index.php"
var urlsplt = url.split(".");
alert (urlsplt[0]); // index



"There is no reason anyone would want a computer in their home." - Ken Olson, President/Founder of Digital Equipment Corp., 1977.

itsallkizza
06-03-2009, 06:32 PM
What if they have a filename of "my.index.php" or a full url like "http://www.mysite.com/index.php"? ;)

Philip M
06-03-2009, 07:03 PM
What if they have a filename of "my.index.php" or a full url like "http://www.mysite.com/index.php"? ;)


var url = "my.index.php"
//var url = "http://www.mysite.com/index.php"
var urlsplt = url.split(".");
var len = urlsplt.length;
var x = urlsplt[len-2]; // com/index
x = x.replace(/(.*\/)/,"");
alert (x); // index

itsallkizza
06-03-2009, 09:07 PM
Ya I was mostly referring to the fact that I had already posted the solution. And your last posted code doesn't return the filename ("my.index") it returns only "index".

ShadowIce
06-03-2009, 10:00 PM
when i use venegal's function, the page returns "'match(...).1' is null or not an object", why is that? I did everything right... I put it in a function called urlname() and added the line in the bottom of the function: "return sPage;" without the quote marks....

ShadowIce
06-03-2009, 10:05 PM
and when i use ANY of ur functions for some reason, my webpage returns a script error... and NO, its NOT my webpage, i've debugged EVERY script file, every function (except the urlname() function), and debugged my webpages using scryptik :)

venegal
06-03-2009, 11:08 PM
Well the regexp depends on there being a filename with an extension at the end of the URL. Is there?

Maybe show your site, that would probably make it very easy to find out why nothing works for you.

itsallkizza
06-03-2009, 11:16 PM
and when i use ANY of ur functions for some reason, my webpage returns a script error... and NO, its NOT my webpage, i've debugged EVERY script file, every function (except the urlname() function), and debugged my webpages using scryptik :)
Well if you copy and paste this into an empty html file it works:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Test</title>
<script type="text/javascript">
// <![CDATA[

function stripExtension(str)
{
var pieces = str.split(".");
if (pieces.length > 1) pieces.pop();
return pieces.join(".");
}

var filename = "index.php";
alert(stripExtension(filename));
filename = "my.index.php";
alert(stripExtension(filename));
filename = "http://www.example.com/my.index.php";
alert(stripExtension(filename));

// ]]>
</script>
</head>
<body>

</body>
</html>

ShadowIce
06-04-2009, 02:02 AM
Well, here's the link, venegal:

http://perchamerica.org/

Try to hurry. because as soon as this gets fixed, im deleting the link for security purposes..

Thanks again! :)

~SI~

venegal
06-04-2009, 02:48 AM
Well the way I see it, the original assumption that there is in fact a filename to parse is simply wrong, as you are trying to use it on your index page.

You will have to change the regexp to

var sPage = sPath.match(/.*\/(.*?)(\.[^\.]*)?$/)[1];
in order to make it cope with both situations.

ShadowIce
06-04-2009, 02:27 PM
ok, now it returns literally nothing..


function urlname(){
var sPath = window.location.pathname;
var sPage = sPath.match(/.*\/(.*?)(\.[^\.]*)?$/)[1];
alert(sPage);
return sPage;
}

~SI~

venegal
06-04-2009, 02:30 PM
Well do some debugging! What value does sPath have?

On your main page it returns an empty string of course, because there's no filename there.

ShadowIce
06-04-2009, 02:35 PM
Its SUPPOSED to return the value of whatever page ur on, like about, or index :)

hence the window.location.pathname :)

venegal
06-04-2009, 02:47 PM
I know what it's supposed to do, but just look at the address bar in your browser. If it says http://perchamerica.org/, how should it ever return index if it isn't in there?

ShadowIce
06-04-2009, 02:49 PM
Right. But thats because Im not testing it on there. Im testing it on my apache server. which is why I need it to work no matter what site im on.

venegal
06-04-2009, 02:56 PM
Well what should I tell you. Do some debugging, for god's sake. If it doesn't work in one environment, alert(sPath) and alert(sPage), post it here, and we can tell you what went wrong.

And get your requirements straight. If you want "index" to be returned when the url ends with a slash, say so.

ShadowIce
06-04-2009, 03:13 PM
ok. i tried it on both pages.. sPath returns "/" on perchamerica.org and "/perchamerica/" on apache. sPage returned nothing on perchamerica.org and nothing on apache. i want the name of the page which is "index" or any other webpage name to be returned.

itsallkizza
06-04-2009, 03:22 PM
And get your requirements straight. If you want "index" to be returned when the url ends with a slash, say so.
For goodness' sake...
Shadow: http://www.example.com/ could be ANY filename. if you don't modify your server settings then it will be index.* as venegal said. explain what you want it to actually do because http://www.example.com/ is a different string (and quite possibly a different physical page) than http://www.example.com/index.php and no amount of RegExp is going to pull the word "index" out of a string that doesn't contain "index" unless you specifically tell it to.

you can find what page is loading using server-side scripts. but as far as i know, there's no way to reliably (ie cross-browser) extrapolate what index page you landed on using purely JS. this of course depends on your server-settings (e.g. are you running any .htaccess url rewrites? in which case JS can definitely NOT tell what page it's on).

ShadowIce
06-04-2009, 03:33 PM
ok. im going to explain for the LAST time... i need to pull the name of ANY webpage BEFORE the .html or .php.............. i tried my best to explain.

if its http://my.website.com/my.website1.html , it would return "my.website1"

if its http://my.website.com/mywebsite.php, it would return "mywebsite"

venegal
06-04-2009, 03:54 PM
That's what it currently does.

But what if its http://my.website.com/? What would it return then? Currently it returns nothing, because there is nothing there. What would you like it to return?

ShadowIce
06-04-2009, 04:00 PM
Id simply want it to return "?", so that the name of my cookie isnt "image_", instead of "image_?"

venegal
06-04-2009, 04:13 PM
No. I asked a question, that's why that question mark is there. It's not part of the URL. So, if the URL is http://my.website.com/, what do you want it to return?

ShadowIce
06-04-2009, 04:28 PM
Id want it to return "my.website" :)

venegal
06-04-2009, 04:34 PM
Well you should have said so in your first post. You can't use window.location.pathname for that, since it doesn't contain the domain name.

I'm tired from running in circles though, but I'm sure somebody else will take it from here.

ShadowIce
06-04-2009, 04:38 PM
u know what? i tried to explain to u and alls u did was yell at me. so u know what? i dont WANT ur help. ill get it elsewhere. this forum sucks anyway.....

evo
06-04-2009, 04:57 PM
Are members are here to provide help, they do so on a free, voluntary basis.

Please refrain from flaming.

As you don't want any further help, topic closed.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum