JavaScriptAn ideal scenario for lightweight Ajax and JavaScript based application is to use one script only. An effective application is not necessarily about the dazzling scripts that you can add in your online application. You need to have a smart application that could cater to what the user needs.
Sometimes, your users only need a simple HTML and JavaScript application wherein it will only have one script. As long as you answer what they need from an online application, you should be able to have a satisfying application.
But there will always be a time that you would need to install more than a single script in your Ajax based application. By creating two or more scripts for the Ajax based application, you should be able to create more functions. This means more interaction with the user. If your webpage would more than one script, the possibility of interaction with the user is almost endless since you can provide more functions.
On the other hand, be careful in using two or more JavaScript scripts. If do not effectively write the scripts, there is a chance that the application will not work at all. It might happen that your scripts will be targeting the same data or would execute the same functions. When this happens, an error will happen which will hamper further processes.
It doesn’t even matter if the actions required by the scripts differ. For example, the first script will only react after the webpage has been loaded and the second will only react when the user clicks on a button in the webpage. At first they could work well since they can be executed in different times. But if they are located in the same page, the first function might have already cached the data which means it could no longer be modified.
If you’re dealing with two or more JavaScript scripts in a single page, first check the target and the function. As much as possible they should never have to target the same data in the same page.
A good solution if you have two conflicting scripts is to combine them. In gist, the solution is to go back to the simplest form of JavaScript page – make it a single script by combining the scripts that you have. By combining those scripts, you should be able to place in sequence the functions that you need in the application and more specifically, on the data you wanted to use.
Here’s an example:
Script #1: onload = “command()”
Script #2: onload = “a different command()”
If you have this form of coding, you might end up with a confused browser which could be problematic. Instead of using that configuration, try this:
onload = “command (); a different command ()”
By stringing the functions, you should be able to execute the scripts separately without any conflict. Of course you can extend the commands to as many as you can. Just make sure that your users have the capability (bandwidth and device) to handle such functions.