home

get_url

operators

strings

Sponsor001

 

Page created 16 May 2005, updated 13 June 2008.

GET URL

This article is about finding the URL of a web page.

USING PHP

The following script returns the location of a web page:

<?php

echo( $_SERVER['PHP_SELF'] );

?>

This returns: " /computer/web/get_url.php".

Which means that the URL for this page is: http://mp3science.com/computer/web/get_url.php

USES

At the top of this page, you will see "Page created 16 May 2005, updated 13 June 2008". Any alteration to this page will automatically change the updated date using the following script:

<?php echo( date( "j F Y", filemtime( "../.." . $_SERVER['PHP_SELF'] ) ) ); ?>

Since this page is located in " /computer/web/ " directory, it is necessary to address the page as:

" ../../computer/web/get_url.php ".

USING JAVASCRIPT

The following script will return various information about the current web page:

<script type = "text/javascript">

//========================== get full path

var url1 = location.href;

alert("href = " + url1);

//============================= get protocol (http)(file)

var url2 = location.protocol;

alert("protocol = " + url2);

//============================== get host

//========if protocol = file host is null

var url3 = location.host;

alert("host = " + url3);

//============================= get path

var url4 = location.pathname;

alert("path = " + url4);

//============================= give value to hash

location.hash = "here";

//============================= get hash

var url4 = location.hash;

alert("hash = " + url4);

</script>

Click here to run the script on this page. If the hash had been set to " top " you would have returned to the top of the page. Once the hash has been set, it will display in the URL. For example:

http://mp3science.com/computer/web/get_url.php#here

The page contains a location for the hash, with the following HTML code:

<A NAME="here"></A>

WHICH SCRIPT

In reality, it makes very little difference whichever script you use. Personally, I would use PHP, because PHP will print a web page, which should display properly in any browser. Whereas some browsers don't display Javascript properly.