Page 1 of 1

2 snips. JS & PHP version. URL Parsers {updated old thre

Posted: Mon Mar 21, 2005 10:49 am
by Chris Corbyn
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=&quote;javascript&quote; type=&quote;text/javascript&quote;>
<!-- 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+:\/\/&#1111;^\/]+)/) //Up to first / after http://
			statusURL = curLocArray&#1111;1]+loc
		} else if (/^\?/.test(loc)) {
			curLocArray = curLoc.match(/^(\w+:\/\/&#1111;^\?]+)/) //Is argument
			statusURL = curLocArray&#1111;1]+loc
		} else if (/^\#/.test(loc)) {
			curLocArray = curLoc.match(/^(\w+:\/\/&#1111;^\#]+)/) //Is anchor name
			statusURL = curLocArray&#1111;1]+loc
		} else if (locArray = loc.match(/^(\.\/)?(.+)?$/)) { //Strip the ./ if exists
			subLoc = locArray&#1111;2]
			curLocArray = curLoc.match(/^(\w+:\/\/.+)\//) //Up to final /
			statusURL = curLocArray&#1111;1]+'/'+subLoc
			
		}
	} else {
		statusURL = loc //Full URL (Absolute)
	}
	window.status = statusURL
	return true
}

// End -->
</script>
Example use:

Code: Select all

<table border=&quote;1&quote; style=&quote;cursor:pointer&quote;>
   <tr>
        <td bgcolor=&quote;#FAFAFA&quote; onClick=&quote;window.location='?mode=x'&quote; onMouseover=&quote;showURL('?mode=x')&quote; onMouseout=&quote;window.status=''&quote;>Click me</td>
    </tr>
    <tr>
        <td bgcolor=&quote;#AAAAFF&quote; onClick=&quote;window.location='/subdir/myfile.php?page=7'&quote; onMouseover=&quote;showURL('/subdir/myfile.php?page=7')&quote; onMouseout=&quote;window.status=''&quote;>Link here</td>
    </tr>
</table>

Posted: Thu May 26, 2005 1:47 pm
by Chris Corbyn
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
				
?>