Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad to have script tag inside div?

What is bad about having a script tag inside div inside body?

I'm dynamically updating a div to reload a javascript code inside a div. Are there any issues to worry about ?


Edit

As @Bergi insisted on seeing the code. Here it is(see below). This div (along with other div(s) containing presentation HTML elements) are updated via AJAX. This script inside div contains maps to do processing of newly loaded HTML elements on page with raw data.

        <div>             <script type="text/javascript">                 var namesMap = <dynamic string from server here>;                 var addressesMap = <dynamic string from server here>;             </script>         </div> 
like image 525
Rajat Gupta Avatar asked Oct 25 '12 19:10

Rajat Gupta


People also ask

Can we use script tag inside body?

JavaScript in <head> or <body> You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

Does it matter where you put the script tag?

The script tag should always be used before the body close or at the bottom in HTML file. The Page will load with HTML and CSS and later JavaScript will load.

Are script tags bad?

In my experience, script tags are bad if they cause your site to load slowly. Site speed actually does have an impact on your appearance in SERPs, but script tags in and of themselves aren't necessarily bad for SEO.

Should script tags be in head or body?

The best practice is to put JavaScript <script> tags just before the closing </body> tag rather than in the <head> section of your HTML. The reason for this is that HTML loads from top to bottom. The head loads first, then the body, and then everything inside the body.


1 Answers

It is perfectly ok to place the <script> tag anywhere in the body of the document.

From here,

The SCRIPT element places a script within a document. This element may appear any number of times in the HEAD or BODY of an HTML document.

However, whenever a <script> tag occurs, it pauses the parsing of the code till the script gets loaded, and executed.

like image 157
Pranav 웃 Avatar answered Sep 21 '22 03:09

Pranav 웃