Reputation: 1501
I have general field in sitecore which can have internal or external link. I need to add target="_blank" for only external link.
I tried setting target window as new browser on click of "Insert External Link" in Sitecore, but no use.
It's ok if I add target=_blank from code also.
Code :
UrlOptions urlOptions = new UrlOptions();
urlOptions.LanguageEmbedding = LanguageEmbedding.Never;
Title = FieldRenderer.Render(item, "Title");
Summary = FieldRenderer.Render(item, "Short Description");
Details = FieldRenderer.Render(item, "Details");
Sitecore.Data.Fields.LinkField lf = item.Fields["TitleUrl"];
if (lf.Url != "")
{
ItemUrl = EFI.Library.SitecoreDataUtil.GetUrlForLinkField(lf, item, urlOptions);
}
else
{
ItemUrl = LinkManager.GetItemUrl(item);
}
Upvotes: 0
Views: 5064
Reputation: 1926
Interestingly, I don't see mention of the <sc:link /> control:
<sc:link runat="server" field="TitleUrl" />
And... that's it. Target for link selected in CMS will apply - of course that depends on your content authors selecting correct target, but gives enough flexibility for "exceptions to the rule". Anyway, just throwing another option out there. Good luck.
Upvotes: 0
Reputation: 4410
I agree with jammykam, if you never want to embed the language it's best to set it in the web.config. That way it'll be consistent across the board (and there's no chance you'll forget).
You can check in code whether your link is internal or external by using lf.IsInternal
.
I think you could probably do something similar to this:
UrlOptions urlOptions = new UrlOptions();
urlOptions.LanguageEmbedding = LanguageEmbedding.Never;
Title = FieldRenderer.Render(item, "Title");
Summary = FieldRenderer.Render(item, "Short Description");
Details = FieldRenderer.Render(item, "Details");
Sitecore.Data.Fields.LinkField lf = item.Fields["TitleUrl"];
if (!lf.IsInternal)
{
lf.Target = "_blank";
}
ItemUrl = FieldRenderer.Render(item, "TitleUrl");
Upvotes: 2
Reputation: 16990
Have you tried the following?
Render a General Link in Sitecore with target="_blank"
If you do not want to embed language you can set it for all links in web.config by setting languageEmbedding="never"
:
<linkManager defaultProvider="sitecore">
<providers>
<clear />
<add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" addAspxExtension="true" alwaysIncludeServerUrl="false" encodeNames="true" languageEmbedding="never" languageLocation="filePath" shortenUrls="true" useDisplayName="false" />
</providers>
</linkManager>
Or you could open the link in a new window using jQuery on all anchor tags that start with http:
$(document).ready(function(){
$('a[href^=http]').click(function(){
window.open(this.href);
return false;
});
});
Upvotes: 5