dsdsdsdsd Posted November 6, 2009 Share Posted November 6, 2009 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; Quote Link to comment https://forums.phpfreaks.com/topic/180551-how-to-get-the-name-of-the-current-webpage/ Share on other sites More sharing options...
dsdsdsdsd Posted November 6, 2009 Author Share Posted November 6, 2009 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 ) ; Quote Link to comment https://forums.phpfreaks.com/topic/180551-how-to-get-the-name-of-the-current-webpage/#findComment-952607 Share on other sites More sharing options...
BillyBoB Posted November 6, 2009 Share Posted November 6, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/180551-how-to-get-the-name-of-the-current-webpage/#findComment-952748 Share on other sites More sharing options...
dsdsdsdsd Posted November 6, 2009 Author Share Posted November 6, 2009 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; Quote Link to comment https://forums.phpfreaks.com/topic/180551-how-to-get-the-name-of-the-current-webpage/#findComment-952783 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.