Jump to content

how to get the name of the current webpage


dsdsdsdsd

Recommended Posts

hello;

 

I have been using :

function get_page( args_URL ) 	  
  { var lvi_last_slash = args_URL.lastIndexOf( "/" )                                      ;
    var lvi_last_dot   =args_URL.lastIndexOf( "." )                                      ;
    var lvn_page      = args_URL.substring ( lvi_last_slash + 1 , lvi_last_dot  ) ;

    return lvn_page ;
  }

alert( get_page( "www.booger.com/index.php" ) ) ; // "index" 

 

and this works for many situations, such as in this simple example;

 

 

but, it seems to me that surely there is some built in JS functionality that can provide this info;

 

 

any thoughts?

 

thanks;

Link to comment
Share on other sites

here is a slightly better way:

 

var lvn_page       = ""                                                                    ;
    
var lvs_URL        = document.URL                                                          ;
var lvi_last_slash = Math.max( lvs_URL.lastIndexOf( "/" ) ,  lvs_URL.lastIndexOf( "\\" ) ) ; // remember, in regex's that a  \  will literalize, so you need 2 of them;
var lvi_last_dot   = lvs_URL.lastIndexOf( "." )                                            ;

if ( lvi_last_slash < lvi_last_dot )
  { lvn_page = lvs_URL.substring ( lvi_last_slash + 1 , lvi_last_dot - 0 ) ;
  } 
else if ( lvi_last_slash > lvi_last_dot )
  { lvn_page = "index" ; 
    // this handles situations where the page is not referenced in the URL, but just the directory,
    //     and in such cases browsers/servers look for a 'index' page .. so there better be one;		
  } ;
  
alert(  lvs_URL + " , " + lvn_page ) ;

Link to comment
Share on other sites

BillyBob,

 

if I am not mistaken the method you posted determines if some string is representative of a URL address or not;

 

but I don't think that this particular example can extract a page name out of a URL address;

 

for instance, if I have: http://www.boo.com/my_page.php, how can I extract the 'my_page' part out of the string;

 

the difficulties arise because you can have:

http://www.boo.com

http://www.boo.com/

http://www.boo.com/my_page.php

http://www.boo.com/my_page.php?a=ihihuh

and I am sure there are dozens more variations;

 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.