This project is read-only.

Open New Window for External HyperLinks

Topics: ASP.NET 2.0
Feb 4, 2009 at 10:34 PM
Edited Feb 4, 2009 at 10:36 PM
Anyone know if there is an extension for this?  Or possibly this can be a light discussion on how one might implement this...  Bassically, any hyperlink on my blog site (pages or posts) that contain hyperlinks to external websites/domains - I would like for blogengine to add a target="_blank" line into the hyperlink so that the user isn't taken away from my site to view the external link/site/domain.  Not sure if I know of a way via to do this site wide.  Any hints/tips would be greeat!  I will also scour the net and see what I find.  Thanks! 
Feb 4, 2009 at 11:58 PM
An extension could definitely handle adding target="_blank" to posts by subscribing to the Post.Serving event.  The BreakPost extension is an example of an extension which handles the Post.Serving event.  I don't see a similar event for pages, however.

Another easy to implement option would be jQuery.  To get jQuery to work within BE, you do have to use jQuery's noConflict() method because both jQuery and BE use the $ JavaScript alias.  If you put the following code in the <head> tag of you site.master file, it should automatically add target="_blank" to all hyperlinks with an HREF beginning with 'http' (assuming internal links don't have href's beginning with http).

<script type="text/javascript" src=""></script>
<script type="text/javascript">

  $j = jQuery.noConflict();
    $j("a[href^='http']").attr("target", "_blank");

This could be refined to only add target="_blank" to hyperlinks within posts, instead of the entire page.  This assumes your posts are within a div containing the 'post' css class (which is how most themes work, I think).

$j(" a[href^='http']").attr("target", "_blank");

And if you did have internal links with HREFs beginning with http, jQuery can check for that too.  The following jQuery statement would add target="_blank" to links with HREF's begin with http, but don't begin with

$j("a[href^='http']:not([href^=''])").attr("target", "_blank");
Feb 5, 2009 at 1:24 AM
I take that back about the Page class not having a Serving event.  It does, but depending on where you try to use it, it needs to be correctly qualified since the 'Page' in Page.Serving may be interpreted as System.Web.UI.Page!


So, you can go the client-side jQuery route, or the server-side Extension route.
Feb 5, 2009 at 1:34 AM
Cool, thanks - will try these out and see what comes up!  wonder if there is a sitewide manner to do this.  Good tip and at least I'm headed in the right direction! 
Feb 5, 2009 at 1:52 AM
If you put that jQuery code in your site.master file (in your theme folder), then it will be sitewide since all your pages use site.master.