PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Mon Mar 21, 2005 10:49 am
I made this handy function because I regularly include onClick events in my page to act like hyperlinks. Unfortunately doing this doesn't show the URL in the status bar like a normal hyperlink would do. This function takes either a relative or absolute URL and builds the full URL from it then writes it to the status bar mimicking what <a href=....> would do. This looks very convincing if use along with style="cursor:pointer". Stick it in between your HEAD tags. It supports all protocols....
Code: Select all
<script language="e;javascript"e; type="e;text/javascript"e;>
<!-- Hide from older browsers
//*************************************************
// Function: URL Constructor, showURL()
// Author: d11wtq (Chris Corbyn)
// Date: March 2005
// Usage: showURL('./somedir?mode=x')
//*************************************************
function showURL(loc) {
statusURL = ''
curLoc = unescape(window.location)
if (!/^\w+:\/\//.test(loc)) { //Has protocol in string
if (/^\//.test(loc)) { // Starts with /
curLocArray = curLoc.match(/^(\w+:\/\/ї^\/]+)/) //Up to first / after http://
statusURL = curLocArrayї1]+loc
} else if (/^\?/.test(loc)) {
curLocArray = curLoc.match(/^(\w+:\/\/ї^\?]+)/) //Is argument
statusURL = curLocArrayї1]+loc
} else if (/^\#/.test(loc)) {
curLocArray = curLoc.match(/^(\w+:\/\/ї^\#]+)/) //Is anchor name
statusURL = curLocArrayї1]+loc
} else if (locArray = loc.match(/^(\.\/)?(.+)?$/)) { //Strip the ./ if exists
subLoc = locArrayї2]
curLocArray = curLoc.match(/^(\w+:\/\/.+)\//) //Up to final /
statusURL = curLocArrayї1]+'/'+subLoc
}
} else {
statusURL = loc //Full URL (Absolute)
}
window.status = statusURL
return true
}
// End -->
</script>
Example use:
Code: Select all
<table border="e;1"e; style="e;cursor:pointer"e;>
<tr>
<td bgcolor="e;#FAFAFA"e; onClick="e;window.location='?mode=x'"e; onMouseover="e;showURL('?mode=x')"e; onMouseout="e;window.status=''"e;>Click me</td>
</tr>
<tr>
<td bgcolor="e;#AAAAFF"e; onClick="e;window.location='/subdir/myfile.php?page=7'"e; onMouseover="e;showURL('/subdir/myfile.php?page=7')"e; onMouseout="e;window.status=''"e;>Link here</td>
</tr>
</table>
Last edited by
Chris Corbyn on Thu May 26, 2005 1:49 pm, edited 1 time in total.
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Thu May 26, 2005 1:47 pm
Hmmm... expanded a bit for parsing relative urls into absolute ones for PHP.
Code: Select all
<?php
function parse_absolute_url($base_url, $link) {
$base_path = $base_url;
//Format the base url given to be a *valid* url
if (!preg_match('/^\w+\:\/\//', $base_path)) {
$base_path = 'http://'.$base_path; //Assume it's http
} //End if
if (preg_match('/^\w+\:\/\/[^\/]*$/', $base_path)) {
$base_path .= '/'; //Append / as the root path
} //End if
//Decide what the link looks like and parse it to the domain URL
if (preg_match('/^\w+\:\/\//', $link)) { //Absolute URL
$url = $link;
} elseif (preg_match('/^\//', $link)) { //Root path
preg_match('/^(\w+\:\/\/[^\/]+)/', $base_path, $url_match);
$url = $url_match[1].$link;
} elseif (preg_match('/^\?/', $link)) { //Query string
preg_match('/^(\w+\:\/\/[^\?]+)/', $base_path, $url_match);
$url = $url_match[1].$link;
} elseif (preg_match('/^\#/', $link)) { //Anchor
preg_match('/^(\w+\:\/\/[^\#]+)/', $base_path, $url_match);
$url = $url_match[1].$link;
} elseif (preg_match('/(?:\.\/)?(.*)$/', $link, $link_bits)) {
preg_match('/(\w+\:\/\/.*)\//', $base_path, $url_match); //Relative Path
$url = $url_match[1].'/'.$link_bits[1];
} //End if
return $url;
}
/* EXAMPLES */
echo parse_absolute_url('google.com/foobar/file.php', '#foobar'); // http://google.com/foobar/file.php#foobar
echo parse_absolute_url('localhost', '?foo=bar&jack=bill'); // http://localhost/?foo=bar&jack=bill
echo parse_absolute_url('http://www.yoursite.com/somefolder/somefile.php?query=values#anchor_here', '/index.php'); // http://www.yoursite.com/index.php
echo parse_absolute_url('https://acb123.co.uk/foobar', './nextdir/file.php'); // https://acb123.co.uk/foobar/nextdir/file.php
echo parse_absolute_url('localhost/dir/file.html', '#top'); // http://localhost/dir/file.html#top
?>