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;

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 ) ;

I found http://www.go4expert.com/forums/showthread.php?t=2262, it has an interesting one line way of doing it.

 

<script>
var url_match = /https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/;
alert(url_match.test("http://www.go4expert.com/forums/showthread.php?t=2262"));
</script>

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;

 

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.