Call function based on url

Discussion in 'JavaScript' started by carl_in_florida, Nov 18, 2007.

  1. #1
    Please help a java idiot.

    I have a function that i would like to be called based on the url.

    something like if url= http://mysite.com/sub/pagename/show then call the function.

    Pagename will vary.

    Can anyone help me code that?
     
    carl_in_florida, Nov 18, 2007 IP
  2. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It's not exactly clear to me what you want, but this would use RegEx and check for similiar criteria you speicified, where pagename can vary and contains letters and numbers
    
    var v = new RegExp(); 
    v.compile("^http://www.mysite.com/sub/[A-Za-z0-9]+/show$"); 
    if (!v.test($url_to_check))
    {
    	foo();
    }
    
    Code (markup):
     
    hogan_h, Nov 18, 2007 IP
  3. xmcp123

    xmcp123 Peon

    Messages:
    876
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $_SERVER['REQUEST_URI']=Your page, inluding folders. I think this includes get variables?
    $_SERVER['SCRIPT_NAME']=Same deal, but probably without get variables.

    Quick function(untested) to extract out the script's name, without get variables
    function getScriptName()
    {
    $s=$_SERVER['SCRIPT_NAME'];
    $spl=explode("/",$s);
    $s=$spl[sizeof($spl)-1];
    $spl=explode("?",$s);
    return(trim($spl[0]));
    }
     
    xmcp123, Nov 18, 2007 IP
  4. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Huh, that's not even the javascript, it's php :confused: , you are in javascript forum :)

     
    hogan_h, Nov 18, 2007 IP
  5. xmcp123

    xmcp123 Peon

    Messages:
    876
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Oops! My bad. I think in PHP, and saw it in the new posts, and just kinda assumed....
    I shall now quietly retreat...
     
    xmcp123, Nov 18, 2007 IP
  6. orielo

    orielo Peon

    Messages:
    175
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    this should return you the current document :

    <script type="text/javascript">
    <!--
    // try this (add http:// if needed)
    function getHost() {
    var url = window.location.href;
    var nohttp = url.split('//')[1];
    var hostPort = nohttp.split('/')[0]
    alert (hostPort)
    }
    getHost();
    </script>
    Code (markup):
     
    orielo, Nov 19, 2007 IP