Wiki style auto link formatting through JS

This is a simple effect that looks pretty awesome and can definatly make your blogging life much easier. (how? because you will never have to type “target=_blank” again, thats how.)

Heres the JavaScript includes

src="/jslib/prototype.js"

src="ubrious.js?domain=blog.ubrio.us"

or

src="ubrious.js?domain=<?=$_SERVER['SERVER_NAME']?>"

And the JavaScript itself

Event.observe(window, 'load', function()
{
	domain = '';
	$A(document.getElementsByTagName("script")).findAll( function(s)
	{
		domain = s.src.match(/ubrious.js(?.*)?$/);
		if(domain)
			domain = domain[1].replace(/?domain=/, '');
	});
	$A(document.getElementsByTagName('a')).each(function(l)
	{
		if(l.target=="_blank" || !l.href.match('/'+domain+'/') && !l.href.match(/^javascript/))
		{
			// attach if outside of your domain, and not a javascript link..
			Element.addClassName(l, 'aexternal');
			l.target="_blank";
		}
	});
});

Finally, the link classes

.aexternal{
	background: url(imgs/aout.gif) right no-repeat;
	padding-right: 12px;
	}



Comments

  1. Ivan May 17th

    Comment Arrow

    Nice


  2. Arion May 17th

    Comment Arrow

    Nice…


  3. Herakles May 17th

    Comment Arrow

    Cool…


Add Yours

  • Author Avatar

    YOU


Comment Arrow



About Author

Rob Hurring

Ruby, Rails, PHP, bash... oh my!