CSS
a.extendedLink
{
text-decoration: none;
width: auto;
height: auto;
padding: 0px;
margin: 0px;
display: block;
}
a.extendedLink:link
{
text-decoration: none;
width: auto;
height: auto;
padding: 0px;
margin: 0px;
display: block;
}
a.extendedLink img
{
width: 30px;
height: 30px;
vertical-align: middle;
padding: 0px;
margin: 0xp;
}
a.extendedLink header
{
border-bottom: 1px solid gray;
font-size: 30px;
}
a.extendedLink description
{
margin-left: 5px;
}
a.extendedLink:hover
{
text-decoration: none;
width: auto;
height: auto;
padding: 0px;
margin: 0px;
display: block;
}
Javascript
$(document).ready(function() {
var map = {
'a.extendedLink[href^="http://"]': /^(http:\/\/[^\/]+).*$/,
'a.extendedLink[href^="https://"]': /^(https:\/\/[^\/]+).*$/
};
$.each(map, function (key, value) {
$(key).each(function ()
{
var link = $(this);
var html = link.html();
var url = link.attr('href');
var title = link.attr("title");
var icon = url.replace(value, '$1') + '/favicon.ico';
console.log(icon);
var header = $('' + html + ' ');
var img = $("
");
var description = $('' + title + ' ');
var imgCheck = new Image();
imgCheck.src = icon;
imgCheck.onerror = function()
{
img.attr("src", "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiSuagEm4fjs5bVXRoCwzxShyphenhyphen2bJWe5DVan-3bZ6aWxHfBDG4YNX_6CCtm_fORme_octD2YPtz8PEXuhe9gtKDoVrvsGc2RC3y_9xOTuQEIrvQsHhdh9yieTNqGe17tBfMZVN62xIU1xwA/s1600/6930ico_question.ico");
}
link.attr("target", "_blank");
link.attr("rel", "nofollow");
link.html("");
header.appendTo(link);
img.appendTo(link);
description.appendTo(link);
});
});
});
Explanation
After the document is ready, for each link with class name 'extendedLink' and a href starting with 'http://':
- Create array map to hrefs (http, https) - map
- For each item in the array, do the following steps for each link(extendedLink)
- a reference to the link will be created - link
- collect the current html - html
- collect the current attribute 'href' - url
- collect the current title - title
- collect the icon location based on the base address - icon
- set the header with the current links html - header
- set the image with the collected icon location - image
- set the description with title - description
- create Image() with icon src and add src default if image has error loading
- set link attribute 'target'
- set link attribute 'rel'
- set link html with header, image, and description
Own experiences...
JQuery andreaslagerkvist
I was posting a reference link to facebook, when I realized the icon wasn't being found. The above code didn't include parsing for [https]. I have changed the above code to reflect this.
ReplyDeleteI created an array to hold the element and regex values. Afterwards, I loop through each item in the array, and foreach element, I return the icon.
var map = {
'a.extendedLink[href^="http://"]': /^(http:\/\/[^\/]+).*$/,
'a.extendedLink[href^="https://"]': /^(https:\/\/[^\/]+).*$/
};
$.each(map, function (key, value) {
$(key).each(function ()
{