add friend or ...? ...">

Better: <a href= "https://stackoverflow.com/test" onclick = "T.actions.addFriend(profileId)" rel="nofollow noreferrer"> add friend </a> or ...?

Better: <a href="/test" onclick="T.actions.addFriend(profileId)">add friend</a>

or

<a href="/test" class="add-friend-button">add friend</a>

      

and then using Javascript select the "add-friend-button" class and add a click event?

+2


a source to share


4 answers


Depends on personal taste. Adding onclick

later will be "better", "modular", etc. However, if your HTML document filled with tags <script>

can be even uglier.



Also read the Wikipedia article Unobstrusive Javascript (UJS) .

+2


a source


"and then use Javascript to select the" add-friend-button "class and add a click event?"



Yes, it's better. You want your markup to be clean (non-intrusive javascript).

+2


a source


If this button is used across multiple pages, be sure to register the event with the. Then you can place the registration in an external js file and include it with every page using this type of button.

The concept is similar to CSS. You only want to define your styling code or js code in one place, whether inline, in a page, or an included file depends on what you are trying to accomplish.

0


a source


Adding "onclick" later is also slower! (I know a little slower so that it doesn't diminish the visual experience of your webpage).

If you only need to add it in one place on your page, you can also put the onclick right on the line, but if you add these onclick events to many parts of your page, I would recommend to be split and unobtrusive (by adding onclick later in the script event or in window.onload).

0


a source







All Articles