Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post

    Need help with getting name of file WITHOUT extension...

    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:

    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~

  • #2
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Just use a regexp:
    Code:
    var sPath = window.location.pathname;
    var sPage = sPath.match(/.*\/(.*?)\./)[1];
    alert(sPage);

  • #3
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    Here ya go:
    Code:
    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));
    Last edited by itsallkizza; 06-03-2009 at 09:10 PM.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,893
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    Code:
    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.

  • #5
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    What if they have a filename of "my.index.php" or a full url like "http://www.mysite.com/index.php"?
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,893
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    Quote Originally Posted by itsallkizza View Post
    What if they have a filename of "my.index.php" or a full url like "http://www.mysite.com/index.php"?
    Code:
    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

  • #7
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    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".
    Last edited by itsallkizza; 06-03-2009 at 09:10 PM.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #8
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post
    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....
    Last edited by ShadowIce; 06-03-2009 at 10:04 PM.

  • #9
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post
    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
    Last edited by ShadowIce; 06-03-2009 at 10:29 PM.

  • #10
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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.

  • #11
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    Quote Originally Posted by ShadowIce View Post
    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:
    Code:
    <!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>
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #12
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post
    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~

  • #13
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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
    Code:
    var sPage = sPath.match(/.*\/(.*?)(\.[^\.]*)?$/)[1];
    in order to make it cope with both situations.

  • #14
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post
    ok, now it returns literally nothing..

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

  • #15
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •