code snippets - HTML - Turn any element into a link:

Started by ThorSummoner, December 22, 2011, 05:47:59 PM

Previous topic - Next topic

ThorSummoner

Ever used anchors to make links and then need to modify link style to make it look good? Well i have, and I've come to the reolization that I can do it better with javascript.
So I did; I used the JQuery framework to simplifly the code, this first bit is the function that makes it work:


<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8">
function redirect(v_location) {if(v_location){window.location = v_location;}else{window.location = "./error/404.html"};};

$(document).ready(function(){
$('*').mouseover(function(){if($(this).attr('href')){$(this).css({'cursor':'pointer'});}});
$('*').mouseout(function(){if($(this).attr('href')){$(this).css({'cursor':'auto'});}});
$('*').click(function(){if($(this).attr('href')){redirect($(this).attr('href'));}});
});
</script>
</head>

This second bit is an example of how to make any element a link.
simply add a href="" attribute to any element, the funtions above will check the mouse's events and see if its over anything designated to be a link and take approprate action;

For example:

<div href="html://www.bing.com">This now is basically a link!</div>


Since this posting ive ramped up some more features, but I fear they would be awefuly confusing to anyone not adept at how html is renderd.
-Thor