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.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Sep 2002
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question String Functions

    I want to find out what is the file name of the page that a user is currently viewing (for e.g. the file name of the page where I am composing this query currently is newthread.php). If the file name is Edit.asp, then do something else......this is the code (this is the onLoad event function)
    Code:
    function getFile(){
        var strURL=window.location
    
        //since the file name will be at the end of the URL, I am using
       //a negative number as the start parameter in the substr function
    
        if(strURL.substr(-9,strURL.length)=="Edit.asp"){
            //do something
        }
        else{
            //do something else
        }
    }
    But the above doesn't work. In fact, some of the JavaScript string functions I tried (like match, length etc.), none of them seem to work since the string has been derived using window.location. Why? Doesn't JavaScript consider the URL of a page as a string object?

    Can someone please tell how do I resolve this?

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Does asp not have a method for getting the current uri? In php you could use
    PHP Code:
    <?php echo $_SERVER['REQUEST_URI']; ?>
    which would give you the current url that you are on. You should be using
    Code:
    var strURL=window.location.href
    That should get things working though I think you should be doing this server side anyways.
    Last edited by _Aerospace_Eng_; 05-13-2007 at 09:51 AM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    Here is an expample:
    Code:
    function getFile(){
    var strURL = self.location.href.split('/');
       if(strURL[strURL.length -1] == "Edit.asp"){
            //do something
        }
        else{
            //do something else
        }
    }

  • #4
    New Coder
    Join Date
    Sep 2002
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks rwedge, your code works exactly as what I wanted but what's wrong with the code I cited in my post? Why doesn't it work?

    Thanks to Aerospace too for his suggestion but sorry to say, Aerospace, I am not fully convinced with your statement that I should be doing this server-side rather than client-side. I feel when & where one needs to use client-side programming & server-side programming entirely depends upon the application he is working on, isn't it?

    Anyway thanks once again to both of you,

    Regards,

    Arpan

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Quote Originally Posted by arpan_de View Post
    Thanks rwedge, your code works exactly as what I wanted but what's wrong with the code I cited in my post? Why doesn't it work?

    Thanks to Aerospace too for his suggestion but sorry to say, Aerospace, I am not fully convinced with your statement that I should be doing this server-side rather than client-side. I feel when & where one needs to use client-side programming & server-side programming entirely depends upon the application he is working on, isn't it?

    Anyway thanks once again to both of you,

    Regards,

    Arpan
    Thats true but you didn't quite tell us what you were working on so how can we know?

    If you weren't too busy trying to say I was wrong you would have noticed why your original script wasn't working. Reread my post again.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    function getFile(){
        var strURL=window.location.href;
        //or
        //var strURL=window.location.pathname;
        //var strURL="<%=Request.ServerVariables("URL")%>";
       
        if(strURL.toLowerCase().indexOf("edit.asp") != -1){
            //do something
        }
        else{
            //do something else
        }
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    why are you referencing location.href when you are looking for part of location.path
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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