January 25, 2019

Automatic hyperlinking.


This may come in handy:

    If You often link to the same places - You might find this concept useful. Suppose Your posts/articles often have a link to one (or more) particular website(s) You frequently refer to. Over the time You may have quite many of such articles in which such hyperlinks are present.

    Now, what if the target address has been changed? All instances of the hyperlink on Your website became useless, leading to "404 error" or another website which is not up-to-date anymore. In order to fix this You need to re-edit this hyperlink in all places where You've used it. However many posts/articles You need to handle - You need to do this as soon as possible in order for Your website to provide a good user-experience (bad hyperlinks don't give a good impression). Unfortunately, it is a pretty time-consuming work - whether You use Blogger Dashboard or Open Live Writer to edit content of Your site. It is still much of a hassle.

    A game changer here would be a small snippet of code which could do all the job for You. You don't need to miss of a find/replace mechanism for Blogger posts anymore. One single thing can solve this well enough - once and for all.

    But what exactly it could do? It works like this: whenever You use a word (or a phrase) which normally You'd like to make a hyperlink - the jQuery will do it for You, automatically. So it saves some time - but the true power of this solution You'll discover in case You'd need to change the link address in the future (if the target website has changed its location). This time the only thing You need to do is to change the address in one place, just one place in Your jQuery code. No need to edit a whole bunch of posts/articles. You edit one place and voilà!: as a result You see that all the links on Your site has been changed accordingly. The code looks as follows:

    $('.post-body:contains(a word or a phrase)').each(function(){
    $(this).html().replace(/a word or a phrase/g,'<a href="" target="_blank">a word or a phrase</a>')

    So in this case You just set a word or a phrase which should automatically be linked to some particular website whenever it is present within Your article text.

    It is important to know that this code works only with so-called "plain text" (a word or a phrase which You don't make a hyperlink by hand) - so it is a good solution to use in case You're gonna to link to some particular place, not doing it "by hand" before. If You, however, already have some hyperlinks on Your site (inserted "by hand" - not by the jQuery mechanism described above) - which need to be edited because are not valid anymore - another code snippet comes in handy:

    $("a[href='']").attr('href', '')

    This time You replace addresses - be careful and use the exact form of the address You want to change - note whether it has "https" or "http", does it include "www" part or not, etc. If You'd like to have 100% certainty that it will fix all the links of Your site, You may consider to clone this code snippet and edit it to match various address variants (https/http, with or without www, etc.).

    No comments:

    Post a Comment