How to open a document in a separate window from a site map
Asked Answered
J

6

5

I was hoping to open a document in a menu control using a sitemap. I am using the following code in the sitemap but get an error. I would like to be able to click on the menu item, have it open the sample doc in a new window, but not to have the original page navigate to a new place (essentially to do nothing on the main page.)

<siteMapNode url="javascript:window.open('Sample.doc','SampleName'); return false" title="FAQs"  description="FAQs" />

Any idea? Is there some javascript I can use that does not require me to register a function on every page?

Jallier answered 6/5, 2010 at 18:50 Comment(0)
J
8

I ended up using the following:

<siteMapNode url="javascript:window.open('Sample.doc','SampleName'); void(0);" title="FAQs"  description="FAQs" />
Jallier answered 6/5, 2010 at 21:51 Comment(0)
G
6

If you handle the OnMenuItemDataBound event on the ASP.NET Menu control, you can set the target attribute on the item there:

MyMenu.MenuItemDataBound += OnMenuItemDataBound

private void OnMenuItemDataBound(object sender, MenuEventArgs e)
{
    // Sets all menu items to open in new windows
    e.Item.Target = "_blank";

    // Uses a 'target' attribute in the XML sitemap if set:
    string targetAttributeValue = ((SiteMapNode)e.Item.DataItem)["target"];
    if (targetAttributeValue != null) {
        e.Item.Target = targetAttributeValue;
    }
}
Gilley answered 7/4, 2014 at 13:33 Comment(0)
D
1
javascript:widow.open

Are you sure you don't mean window.open?
I don't think your script is that much related to widows ;)

Dispassionate answered 6/5, 2010 at 18:55 Comment(1)
Hah! I did mean window.open. I've updated my question (that wasn't my issue.) I have been trying so many things I got sloppy... thanks.Jallier
L
1

You will need to find out which control is consuming the sitemap file and then catch the event generated from that control.

Lexine answered 6/5, 2010 at 19:27 Comment(0)
A
0

Why not just use the target attribute?

<siteMapNode url="Sample.doc" target="_blank" title="FAQs"  description="FAQs" />
Anitraaniweta answered 17/8, 2012 at 0:22 Comment(2)
The target attribute doesn't get applied to the link tag generated by the menu controlGilley
Up voted this because I was wondering the same thing. So the question is useful even if only for discounting a solution.Precursory
A
0

target="_blank" on the source page was lost when the page rendered for me. I had to do it in the code behind like so:

private void Menu1_PreRender(object sender, EventArgs e)
{
    if (!IsPostBack) {
        MenuItem FAQsItem = new MenuItem("FAQs");
        FAQsItem.NavigateUrl = "~/Sample.doc"; //You'll need to figure out your correct URL
        FAQsItem.Target = "_blank";
        Menu1.Items.Add(FAQsItem);
    }
}

Note: I added this to the Menu's PreRender because I just wanted to add it to the end of the other MenuItems that were in the source page and did not need target="_blank".

Aspic answered 4/3, 2016 at 22:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.