开发者

Conditional HTML need suggestion

What will be better of these 2 cases. Just making sure I am using best practice.

<% if(isEdit){ %>
<input type="text" id="tabtitle" name="title" value=<%=Content%>/>
<%}else{%>
<input type="text" id="title" nam开发者_运维百科e="title"/>
<%}%>

OR

 $(document).ready(function() { 
<% if(isEdit){ %>
   $("#title").val("<%=Content%>");
<%}%>
});


The second uses JavaScript / JQuery to modify the element. Since adding client-side code has no advantage I would take the first approach which is server-side only. Although without seeing the bigger picture it's hard to endorse this approach particularly either.


I would go by the first approach because the conditional is there anyway, and the javascript doesn't add anything to the functionality. It's just a forced approach.

Additionally if you use server side, your code is going to be correct the time it hits the browser, and there will be no lag for the javacsript function to be called. Sometimes there's a flash, or a wipe when you change dom through javascript... You will be better without it.


I'll prefer the first approach as Javascript is usually disabled by some users for security reasons


I would go with Pointy's suggestion and use the expression language (EL) in your JSP. You could just put:

<input type="text" id="${titleid}" name="title" value="${Content}"/>

Set your titleid property to "title" or "tabtitle" and Content to "" or your content. I haven't tested it but I believe that would accomplish the same thing.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜