Open external web site in new window

Topics: Controls
Feb 16, 2014 at 3:05 AM
I'm a BlogEngine newbie, so please cut me some slack if this is an obvious question, or one that has been answered many times before. I did search for an answer, and nothing came up.

I have created a page that contains several links to external web sites, and when the reader clicks on these links, I want them to open in a new window.

Is there a widget, or some other technique, that accomplishes this? Or do I just have to hand-code it in my page?

I'm a little reluctant to start tweaking the underlying ASP pages at this point, but am capable of doing so, if that's the only way,

Thanks a bunch.
Feb 16, 2014 at 8:26 AM
Edited Feb 16, 2014 at 8:28 AM
Hi Malcolmtent,

This can be easily done with jquery like this:
<script  type="text/javascript">
$("a[href^='http:']:not([href*='" + window.location.host + "'])").each(function() {               
        $(this).attr("target", "_blank");
    });
})
</script>
Note: 43 = "plus sign"
Source: http://www.dotnetcurry.com/showarticle.aspx?ID=510

What the script does above is creates a "target_blank" for any link on your website that are "external" links
which causes it to open either in a new tab or window when a user clicks on them.

Doing it this way is "soft coding" it so that you don't have to worry about editing the links yourself.

What I would do is put that script tag in your master page, or even better create an external java-script file
like openNewWindow.js then drop that in to your /scripts folder.

That way it is applied to any external links on your website.


Have a Great day!

Brian Davis

http://www.BloggersOnline.Com