Can you have 2 url's that point to the same page in an asp:Menu control?

后端 未结 4 2103
甜味超标
甜味超标 2021-02-19 19:36

I have an asp:Menu and it contains a top level menu item that points to http://www.example.com/one.aspx. When you hover over the top level menu item, it shows a dropdown and on

4条回答
  •  隐瞒了意图╮
    2021-02-19 19:57

    I found out my solution in the comment above only worked on Firefox, this is an improved solution to make the page refresh even if you click on the same link as the current one but with an hash tag:

    $(document).ready(function () { // refresh page when clicking menu item with current address $('#<%=aspMenu.ClientID%> a').click(function () {
       var currentUrl = location.pathname;
                    var clickedUrl = $(this).attr('href');
                    if (currentUrl.indexOf('#') != -1 || clickedUrl.indexOf('#') != -1){
                        if (currentUrl.indexOf('#') != -1)
                            currentUrl = currentUrl.substring(0, currentUrl.indexOf('#'));
                        if (clickedUrl.indexOf('#') != -1)
                            clickedUrl = clickedUrl.substring(0, clickedUrl.indexOf('#'));
                        if (currentUrl == clickedUrl)
                            location.reload();
     }); });
    

    Or even better: removing the anchors with the digits from the hrefs on document ready:

        $(document).ready(function () {
            $('#<%=aspMenu.ClientID%> a').each(function () {
                var re = /#\d/
                var url = $(this).attr('href');
                $(this).attr('href', url.replace(re, ""));
            });
        });
    

提交回复
热议问题