javascript - How to set the id of a div as cookie? - Stack Overflow

I have got a task to highlight the selected menu while refreshing the page. For that I want to use cook

I have got a task to highlight the selected menu while refreshing the page. For that I want to use cookie. Html code is

<div class="menuBar">
            <div class="menuHeader ui-corner-top">
             <span><a href="#" onclick="Home()" id="home">Home</a></span>
            </div>
            <div class="menuHeader ui-corner-top">
             <span><a href="#" onclick="NewTransaction()" id="newtransaction">New Transaction</a></span>
            </div>
</div>

Javascript file is

function Home() {
            window.location.href = "../../home/wele";
        }
        function NewTransaction() {
            window.location.href = "../../EnergyCatagory/index";
        }

But I have a code to set the menu as selected.But its not a good way.How can I pass the value of the selected menu to the page while refreshing? this is the code for highlighting the menu

 $(document).ready(function () {
                var currentUrl = window.location.href;
                if (currentUrl.indexOf("home/wele") !== -1) {
                    $("#home").parent().parent().addClass('menuHeaderActive');
                }
                else if (currentUrl.indexOf("EnergyCatagory/index") !== -1) {
                    $("#newtransaction").parent().parent().addClass('menuHeaderActive');
                }
                else if (currentUrl.indexOf("portfolio/Index") !== -1) {
                    $("#portfolio").parent().parent().addClass('menuHeaderActive');
                }
            });

I have got a task to highlight the selected menu while refreshing the page. For that I want to use cookie. Html code is

<div class="menuBar">
            <div class="menuHeader ui-corner-top">
             <span><a href="#" onclick="Home()" id="home">Home</a></span>
            </div>
            <div class="menuHeader ui-corner-top">
             <span><a href="#" onclick="NewTransaction()" id="newtransaction">New Transaction</a></span>
            </div>
</div>

Javascript file is

function Home() {
            window.location.href = "../../home/wele";
        }
        function NewTransaction() {
            window.location.href = "../../EnergyCatagory/index";
        }

But I have a code to set the menu as selected.But its not a good way.How can I pass the value of the selected menu to the page while refreshing? this is the code for highlighting the menu

 $(document).ready(function () {
                var currentUrl = window.location.href;
                if (currentUrl.indexOf("home/wele") !== -1) {
                    $("#home").parent().parent().addClass('menuHeaderActive');
                }
                else if (currentUrl.indexOf("EnergyCatagory/index") !== -1) {
                    $("#newtransaction").parent().parent().addClass('menuHeaderActive');
                }
                else if (currentUrl.indexOf("portfolio/Index") !== -1) {
                    $("#portfolio").parent().parent().addClass('menuHeaderActive');
                }
            });
Share Improve this question edited Apr 3, 2013 at 8:10 Nithin Viswanathan asked Apr 3, 2013 at 8:02 Nithin ViswanathanNithin Viswanathan 3,2837 gold badges45 silver badges85 bronze badges 1
  • @MatthewAgustin, can any code be "non semantic"? For me the answer is no. A mountain is non semantic, as is a laundry basket. – Beetroot-Beetroot Commented Apr 3, 2013 at 8:40
Add a ment  | 

3 Answers 3

Reset to default 4

Give id for every menu item.

Then in the onclick() function of menu item set that id as cookie.

Set cookie

$.cookie("selectedId", $(this).attr('id'));

In document.ready function add selected class to the element with id obtained from cookie

You can implement it like this:

function goToLocation(sLocation, id) {
   $.cookie("activediv", id);
   window.location.href = sLocation;
}

in html:

<a href="#" onclick="goToLocation('../../home/wele', 'home')" id="home">Home</a>

in jQuery ready:

$('#' + $.cookie("activediv")).parent().parent().addClass('menuHeaderActive');

Try to set cookises using jQuery as:

$.cookie("name", "value");

More you can read here:How do I set/unset cookie with jQuery?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744726250a4590196.html

相关推荐

  • javascript - How to set the id of a div as cookie? - Stack Overflow

    I have got a task to highlight the selected menu while refreshing the page. For that I want to use cook

    23小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信