admin管理员组文章数量:1435859
I have this object below:
I thought about putting the messages in html like this
<div id = "text_identifier">text_here</div> // div is hidden.
and then using getElementById to pull the message into JavaScript and finally display to the user in the correct element ( the parameter element in Message
)
Two related questions?
Is it bad practice to have content in JavaScript like this?
If I do move the messages into the HTML, is the method I described above best practice?
/**
*Message
*/
var Message = function( element )
{
var messages =
{
name: 'Please enter a valid name',
email: 'Please enter a valid email',
pass: 'Please enter passoword, 6-40 characters',
url: 'Please enter a valid url',
title: 'Please enter a valid title',
tweet: 'Please enter a valid tweet',
empty: 'Please plete all fields',
email_s: 'Please enter a valid email.',
same: 'Please make emails equal',
taken: 'Sorry, that email is taken',
validate: 'Please contact <a class="d" href="mailto:[email protected]">support</a> to reset your password',
}
this.display = function( type )
{
element.innerHTML = messages[ type ];
new Effects().fade( element, 'down', 4000 );
}
};
I have this object below:
I thought about putting the messages in html like this
<div id = "text_identifier">text_here</div> // div is hidden.
and then using getElementById to pull the message into JavaScript and finally display to the user in the correct element ( the parameter element in Message
)
Two related questions?
Is it bad practice to have content in JavaScript like this?
If I do move the messages into the HTML, is the method I described above best practice?
/**
*Message
*/
var Message = function( element )
{
var messages =
{
name: 'Please enter a valid name',
email: 'Please enter a valid email',
pass: 'Please enter passoword, 6-40 characters',
url: 'Please enter a valid url',
title: 'Please enter a valid title',
tweet: 'Please enter a valid tweet',
empty: 'Please plete all fields',
email_s: 'Please enter a valid email.',
same: 'Please make emails equal',
taken: 'Sorry, that email is taken',
validate: 'Please contact <a class="d" href="mailto:[email protected]">support</a> to reset your password',
}
this.display = function( type )
{
element.innerHTML = messages[ type ];
new Effects().fade( element, 'down', 4000 );
}
};
Share
Improve this question
edited Aug 16, 2012 at 17:07
casperOne
74.6k19 gold badges189 silver badges260 bronze badges
asked Apr 9, 2012 at 18:32
user656925user656925
3 Answers
Reset to default 1It depends how you like to do that. Often JavaScript isn't the important thing, it is the php backend part. So integrate it how it is easy readable in your system. :)
I would use the HTML data-*
Attribute for each element.
if you got many inputs you can make it like that:
<input type="text" name="name" data-error-message="Please enter a valid name">
<input type="text" name="email" data-error-message="Please enter a valid email">
<input type="text" name="pass" data-error-message="Please enter passoword, 6-40 character">
and when you are selecting the values, you can also select the error messages :)
I'm sure you know how to use javascript based on what you said thus far, in practice the method you are using is absolutely fine, and should not cause you any harm, I have added a link just incase you need a reference
http://www.javascripttoolbox./bestpractices/
I think it's really a preference thing. You could have it in the HTML and only show a div with a particular class (i.e. a div that says "Sorry, that email is taken" has the class .taken and you show that div when that error triggers).
There may be arguments about performance one way or the other, but I see them as pretty insignificant on this scale, and if anything your method may be slightly better. It gives you one unified place to edit messages, and a simple clean way of calling those messages to the front.
So really, a matter of opinion/preference.
本文标签: User Error MessagesHTML data vs JavaScriptStack Overflow
版权声明:本文标题:User Error Messages | HTML data vs JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745675762a2669841.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论