Should I be incorporating JavaScript within a partial considered a good practice?

Our current design includes multiple partials combined with scripts that act as client-side controllers, all loaded through ajax requests.

For instance:

employee.jsp

<div id="employee">
  <input type="text" name="fullname" />
  <input type="submit" value="Submit" id="employee-submit" data-bind="click : submit"/>
</div>
<script src="employee.js"></script>

An issue arising from this approach is the persistent caching of scripts. If caching is not a concern, why would avoiding this practice be advisable? Are other web developers utilizing this method?

Answer №1

One of the primary reasons to avoid placing script tags in the middle of your content, unless utilizing the async and defer attributes (which are not universally supported), is that they have the potential to obstruct the rendering of anything that comes after them. Since there is no way for the browser to guarantee that there won't be a document.write within the script tag that produces HTML output, it is necessary for the browser to download and execute the script before continuing to parse and display the remainder of the webpage. While some browsers may attempt to optimize this process by assuming the script will not alter the output significantly, others take a more cautious approach.

Embedding a script tag with an external src in your content is generally discouraged due to performance concerns. However, this issue does not apply to inline JavaScript, such as defining variables or adding event handlers to dynamically generated elements. Although not the most elegant solution, these practices incur a much lower performance overhead. Alternatively, inline scripts can also be utilized to load external scripts at a later time, similar to how Google Analytics loads its extensive tracking script.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Attempting the transformation of jQuery ajax requests to Angular's http service

I am looking to transition my existing mobile application from using jquery ajax to angularjs for handling user authentication with server-side validation. Here is the original jquery ajax code: function validateStaffUser(username, password) { var re ...

Logging into a router's network using PHP and Ajax

In the development of a system management website, we are implementing features that allow for changing the wifi channel and restarting or shutting down the router. Currently, our Ajax functions can perform these tasks if the router does not have a passwor ...

Unable to display icon using the fontawesome react-js library

import "./App.css"; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' function App() { return ( <div className="container"> <input type="text" className="form-control" placeholder ...

Alert: Debugger now supports asynchronous stack traces in Node.js

I am currently facing an issue while trying to insert a document into my MongoDB collection. When I execute the program in the VS-Code IDE, I encounter the following error message: << C:\Program Files (x86)\nodejs\node.exe .\DBOp ...

In order for Angular forms to be considered valid, they must fall into one of two form

I am attempting to create a dynamic angular form where the user must enter either A or B. A represents a unique identifier, while B consists of a set of values that correspond to that identifier. My goal is to validate the form as valid if either A is ente ...

Tips for effectively utilizing foreach loops in JQuery

I've encountered an issue while displaying a list of data in a table using Smarty templates and foreach loops. I'm trying to make each line clickable to show new data below it, but currently, this functionality only works for the first line. Is t ...

Changing the src attribute of an <img> tag using Jquery seems to be unresponsive

Hey everyone, I created a simple slider using JqueryUI. When the value falls within a certain range, I generate an Image URL and then attempt to change the src attribute. I am attempting to modify four images. You can view the jsFiddle here. I believe t ...

Error encountered in contact form, specifically related to the 'email' field

I'm encountering an issue with a contact form field that utilizes AJAX for form submission and PHP. The problem I'm facing is that whenever the form is submitted, I receive an error message stating "the request was successful, but the form was n ...

Code igniter validation with Ajax works well, especially when the error function is triggered during the validation->run process

I am currently working on creating a validation form using Ajax. Everything works perfectly when the Textbox is empty and the correct errors are displayed. However, when I fill out the form, the Ajax script stops working and goes to the error function inst ...

What are the steps to reset reCAPTCHA once a user verifies their identity and it times out

I'm currently working on understanding this code snippet: function recaptchaCallback() { $('#submitBtn').removeClass('btn-hide'); $('#submitBtn').addClass('btn-show'); if (grecaptcha.getResponse() == &a ...

Making Mat-Tab Table Headers Sticky in Angular

I'm facing an issue in my application where I have a screen with 3 tabs. One of these tabs contains a table with a large number of rows, and I want to make the headers of this table sticky so that they remain visible when the user scrolls down. Despit ...

Verifying if any of the entries in a specific index contain a null value

Here is the structure I am working with: { "subs": [ { "status": "1", "run_settings": null, "ward": "/asda/asd/ada" "value": null, "name": null }, { "status": "0", "run_settings": null, "ward": ...

Stop Next.js from rendering undefined routes

In my nextjs application, I have set up routes for /vendor and /admin. However, I noticed that even though I haven't defined a route for /admin/something, NextJS still automatically renders the admin page for it. Is there a way to return a not found p ...

Can the renderWith function of a column in angular-datatables retrieve a value from a promise?

Can I trigger a call to a service or filter that retrieves a promise from the renderWith() function of a column? When I attempt this, the result always appears as "[object Object]". vm.dtInstance = {}; vm.dtOptions = DTOptionsBuilder.fromFnPromise(MySe ...

Exploring the world of ASP .NET development with the powerful Sonar

We're currently working on an ASP .NET project and are looking for a way to analyze JavaScript files on-the-fly. Unfortunately, SonarLint only offers analysis for C# files. The incremental analysis feature seems to have been phased out, and issues ana ...

Filter out any items from the JSON data that include a designated keyword

Can anyone suggest the most efficient way to filter out objects from JSON data that contain a specific term or keyword? Here's an example of my JSON data: json = [{ "name":"John", "age":30, "cars":"BMW" }, { "name":"Micheal", "age":30, "cars": ...

Set the input value to null when the ng-hide directive evaluates to true in AngularJS

Hello, I am looking for a way to clear the input text fields when they are hidden using ng-hide="true". I need a solution that dynamically works for multiple hidden inputs. For example, if someone selects "Yes" and enters a child count, then switches to ...

Having trouble with the "initSdk" property being undefined in your React Native Appsflyer integration?

I'm currently facing an issue while trying to integrate AppsFlyer into a react native application. The error I am encountering is "Cannot read property 'initSdk' of undefined" Initially, I imported the react-native-appsflyer module as shown ...

Issue with Gijgo grid not updating properly during change event

I'm currently working on an MVC 5 application and I've hit a roadblock with a particular view. This view contains a dropdown menu and a grid (Gijgo-grid). The grid's content is supposed to change based on the selected value from the dropdown ...

Sending JSON data from a Django view to a JavaScript function

I am trying to pass JSON data to JavaScript. I need the JSON structure to be like this: data: [ { value: 335, name: 'Coding' }, { value: 310, name: 'Database ...