$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>User Error Messages | HTML data vs JavaScript - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

User Error Messages | HTML data vs JavaScript - Stack Overflow

matteradmin44PV0评论

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
Add a ment  | 

3 Answers 3

Reset to default 1

It 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.

Post a comment

comment list (0)

  1. No comments so far