MENUITEM CALL JAVASCRIPT FUNCTION

Tanuj Kumar

Total Post:134

Points:940
Posted by  Tanuj Kumar
JQuery 
 1091  View(s)
Ratings:
Rate this:

Dynamically building menu items and need to call a javascript function from one of them.

 

    function popWin(url) {

        var winHandle = window.open(url, '_blank', 'width=1000,height=700,resizable=yes,top=5,left=5,scrollbars=yes,status=yes');

    }

 

 <asp:Menu ID="Menu1" runat="server" StaticDisplayLevels="8" RenderingMode="List" OnMenuItemClick="Menu1_MenuItemClick" CssClass="menu" Width="150px">

 </asp:Menu>

The code behind:

 

        string url = "/somepage.aspx";

        MenuItem child = new MenuItem();

        child.Text = "Some Text";

        child.NavigateUrl = "javascript:popWin('" + url + "');";

        //child.NavigateUrl = "javascript:window.open('somepage.aspx');";

        Menu1.Items[1].ChildItems.Add(child);

Just having some trouble calling the javascript function.

The window.open will work but an extra page opens and I need more control over the window opening.

 

The reason this needs to be done this way is there's an iFrame on the page that's loading different pages. The RegisterStartupScript was being used in the Menu1_MenuItemClick event but it was refreshing the source of the iFrame.

 

Thanks for any suggestions.

  1. Jeet Verma

    Post:110

    Points:776
    Re: MenuItem call javascript function

    After coming back to this problem and testing a span tag in html with an onclick event, the problem was simply a bit of syntax:

     

            child = new MenuItem();

            string win = "/SomePage.aspx";

            string script = "popWin('" + win + "');";

            string text = "<span style=\"cursor:pointer;\" onclick=\"" + script + "\">Some Text</span>";

            //Below is how it should render

            //<span style="cursor:pointer;" onclick="popWin('/SomePage.aspx');">Some Text</span>

            child.Text = text;

            child.Selectable = false;

            Menu1.Items[1].ChildItems.Add(child);

    Thought I would post this just in case anyone else runs into the same issue.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!