JavaScriptCalling files that contain functions is a common feature in various programming languages. ASP, CSS and PHP have these features wherein developers simply tag the filename within the code in order to a launch a separate function. Implementation of this technique is simpler and will never require extra configuration.
But this setting is different with JavaScript. Developers can't just call a .js file within the code and expect the separate file to execute. What might happen when this is done is that the .js file can be retrieved and read but not executed. The JavaScript engine in various browsers do not have the capability to read a retrieved file. Part of this practice is security - because of the potential unsecured source, the engine was prevented to read the external file.
To deal with this problem, some solutions were provided. Two possible, yet awkward, solutions can be used:
1. Create a separate webpage for external file - developers can create a webpage dedicated for .js. This will serve as a hub for the JavaScript file so that the engine will return with the expected function.
2. Create additional control within the function - of course developers can always create another command within the function that will retrieve and open the function. The additional code will be added to the function to effectively run the .js files.
While these two functions can extract data from .js, they are often tedious. Each function where the .js file is extracted have to be tweaked just to accommodate the .js file.
Another technique developers can use to extract .js data with relative ease is to use the "require" technique. This type of calling external .js file actually forces the JavaScript engine to extract and read the file.
The function would go this way:
• First, developers create the code with easy to recognize JavaScript file such as default.js.
• Secondly, developers can attach a line that goes this way: PO.Require(file destination). As the name suggests, the function is practically transformed since it will never work without the requiring function.
When using this technique, it's important to store the code within the same domain. External .js can be a good option but for security sake, you should avoid getting your needed function from 3rd party and unknown sources. You're practically giving away the keys to your website.
The main advantage of this technique in extracting .js files within the application is the ability to launch without requiring additional resources. As long as the source is properly defined, the function will work with fewer challenges.
However, the function also shares some challenges in asking for functions outside the codes. Among them is browser incompatibilities especially when the external .js file was never tested in various browsers. This might prove to be challenging to some developers who opted to create the application without using libraries. But this disadvantage can be easily dealt with when the function uses a known JavaScript or Ajax library that can easily work with various browsers.
| Comments |
|---|