How to open a URL property link in a new window?

Vote:
 

I have this property:

public virtual Url Link { get; set; }

I want it to have an option in CMS to be able to open in new window,

How can I do it?

Please help

#227047
Aug 26, 2020 13:30
Vote:
 

Use LinkItemCollection instead of Url.

public virtual LinkItemCollection LinkItem { get; set; }

#227053
Aug 26, 2020 13:59
Vote:
 

Hi, 

I can't use LinkItemCollection. Please suggest if you have some other option.

#227054
Aug 26, 2020 14:02
Vote:
 

I think what you can do the best is to have a SelectionFactory in place. That will act as the link should open in a new window/not.

Step 1

 public class LinkItemTarget : ISelectionFactory
    {
        public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
        {
            return new ISelectItem[]
            {
                new SelectItem(){Text = "", Value = "" },
                new SelectItem(){Text = "Open the link in a new window", Value = "_blank" },
                new SelectItem() { Text = "Open the link in the whole window", Value = "_top" },
            };
        }
    }

Step 2 - Decorate it on a different property specifically for links

        [CultureSpecific]
        [Display(Name = "Block Link Target", Description = "target for the Link", GroupName = SystemTabNames.Content, Order = 11)]
        [SelectOne(SelectionFactoryType = typeof(LinkItemTarget))]
        public virtual string BlockLinkTarget { get; set; }

Step 3 - Using it on the page where your link is - 

    var linkTarget = !string.IsNullOrEmpty(Model.BlockLinkTarget) ? string.Format("target={0}", Model.BlockLinkTarget) : string.Empty;
 <a href="someLinkUrl" @Html.Raw(linkTarget)>.......</a>

The target in step 3 would set them to be the value that you choose from Episerver.

Hope it helps.

#227245
Aug 31, 2020 10:30
Vote:
 

Hi Tanvi

Episerver recently introduced LinkItem in the Episerver Lab-On https://world.episerver.com/blogs/grzegorz-wiechec/dates/2020/6/episerver-labs---linkproperty/

Hopefully, it solves your problem.

#227295
Sep 01, 2020 2:33
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.