Javascript / jquery best practices

In my work, we discussed what is best practice. We have one or more tags. We want the tag to trigger the JavaScript function when clicked. To have this functionality, we could bind the event to tags via jQuery, or we could call the inline function in the a tag. What are the pros and cons for each of these decisions, and what is the best way to do it.

Question 2: Usually we also have a JavaScript file with our JavaScript / jQuery located on our main page. Is there any overhead to running jQuery selectors on every page?

+3
source share
5 answers

Question 1

I would suggest you see:

JavaScript ,
Javascript

:

  • ( " " ) -     /

  • , JavaScript (, )

  • ,      JavaScript

2

javascript , , , , .

+2

1:. , javascript HTML . . , , , , HTML - HTML-, javascript JS.

, , , , .

, . jQuery DOMReady. , DOM - , , , . inline , HEAD, , .

- DOMReady ( , ), , , , . javascript:void(0); # href, . href #, return false, , , DOMReady javascript . # , , "" , .

2: , . script , . , , - , . script, URL-, . location.href , , . , , , . .

live, , , DOM . ( Nick)

+1

javascript, .

" JavaScript" - JavaScript , . , :

( " " ) - /

JavaScript (, )

, JavaScript

.

№ 2 , , . , , , .

0

.

<a /> , , . , <a href="http://google.com" onclick="…" >…</a> false, . ( , <a href="#" />, , javascript.)

, jquery , , .

Javascript , jquery.

0

JS , Inline JS, jQuery.

, , jQuery - . . . $('div.content a.link_btn') is a lot faster then $('.link_btn')

0

Source: https://habr.com/ru/post/1751589/


All Articles