Setting a permanent link with jQuery

The problem with static web sites is that they cannot automatically generate a permanent link for their pages. Generally this link is inserted within the main title of the page, for example a blog post. Fortunately, we can easily overcome this problem by using jQuery. More precisely, we're going to generate a permanent link using the href property of the location object. Here's how:

$(document).ready(function() {

  var url = location.href;
  var permalink = '<a href="' + url + '"></a>';
  
  $('div.post').each(function() {
  
    var $post = $(this);
    var $postTitle = $post.find('h2.post-title');
    
    $postTitle.wrapInner(permalink);
  
  
  });
  

});

We use the wrapInner() method to wrap the textual content of our post title with the newly created permanent link. You can see the demo below.

Demo

Live demo

This entry was posted in by Gabriele Romanato. Bookmark the permalink.

Leave a Reply

Note: Only a member of this blog may post a comment.