Is there any good reason for javascript to be inline

I built a site. At some point, I noticed that the IE display was a little broken, and Chrome did almost nothing except the body tag (empty), and FF looked good.

After I threw my keyboard around the room and hit my head with my mouse, I found a problem. I left (don’t ask how and why, maybe there was some kind of lightning speed error and insertion) HTML comment closed in an inline script block.

<script type="text/javascript"> <!-- ... </script> 

I assume (not verified) that the problem would not have occurred or would have manifested itself much more noticeably if the script had been external. Anyway, I thought, is there ever a time when you have a really good reason to write an inline script ??

+5
javascript html scripting inline
source share
9 answers
+9
source share

If you want your Javascript to start as early as possible, it might make sense to enable embedded Javascript, as it will be executed before all other HTTP requests are sure to be completed.

And in some cases, you enable Javascript from a third-party provider, and you really have no choice. Some ad systems, as well as Google Analytics, spring, so as not to worry.

+3
source share

If the script needs to be dynamically generated (say, on a PHP or ASP.NET MVC page), this will be one of the reasons for its inline :-)

+2
source share

Depends on how much JS you plan to write. If you write many support routines (many validation checks, word processing, animation and effects), then it makes sense to have the code in a separate file. This allows code reuse and removes a lot of unwanted information from your HTML page.

On the other hand, there is no need to insert 10 lines of code or one function (there is a problem with updating in JS) in a separate file. It will also load somewhat faster, since the browser does not need to make an additional HTTP request to download a separate JS file.

+2
source share

Most XSS vulnerabilities can only be exploited using embedded javascript.

0
source share

This is not necessarily a good reason, but pages will load faster. For this purpose, sometimes even when you write a script in another file, you want it to appear as inline on the client side.

0
source share

I sometimes place javascript inline on pages that partially reload (to bind some events to newly added form fields) and / or pages that use some kind of unique javascript that I will not use on any other page.

0
source share

Having many external scripts can ultimately slow down the page, as the browser must call each file separately. Combining JavaScript into a single file or per page can sometimes alleviate this problem.

On the other hand, I believe that the browser can cache the script file as soon as it was called for the first time, so if you have a lot of the same code on your site, the external path is the way to go.

0
source share

I work a lot on what's called Flex, which combines XML and ActionScript to create the final bytecode. It is ALWAYS best to separate them as much as possible. This way you can very clearly and easily separate the view (HTML or MXML in my case) from the controller (script)

It also means that you don’t have to worry about viewing five files for one line of code - all your code is in one place.

0
source share

All Articles