开发者

Is it more professional to set selected menu item class="selected" server side or using JavaScript?

开发者 https://www.devze.com 2023-03-13 11:46 出处:网络
I have a menu where the selected item (whose href is equal to the current page) needs to look different to the other menu items. I can either:

I have a menu where the selected item (whose href is equal to the current page) needs to look different to the other menu items. I can either:

  • Compare each menu item href to window.location using JavaScript and add class="selected" to the right element on DOM load, or

  • Serve only the selected menu item with class="selected" by checking each href against something like Request.ServerVariables(开发者_运维问答"URL") server side.

This must be a common situation - which method is generally preferred? To me the second option is probably cleaner but the menu ASP page is less maintainable compared to the first option which is HTML only.


On the client side you'll have a little time when there is no selected menu (it'll increase if you load external script after you displayed your menu). If someone has an old computer with ie6 it can be really really slow.

Javascript is used to make animations, server request (limit those as much as it can with validations), here you are preparing your document so for me it's a server responsibility.

I always try to limit the executed javascript on the "load" event, it there is to much stuff executing it can really decrease the user experience.


Whenever possible, I try to handle that kind of thing server-side as much as possible. That way, it'll still function as expected for users with Javascript disabled, as well as avoiding the quick flash of unstyled content remi mentioned.

0

精彩评论

暂无评论...
验证码 换一张
取 消