| Dynamically Loading external JavaScript file |
|
|
| Written by Administrator | |
| Tuesday, 10 April 2007 | |
|
As a web designer you may face a problem of how to dynamically insert JavaScript inside your web pages. Since Dynamic Object Model is now getting more popular among web designers, It is important to find a good way for Inserting or even executing JavaScript dynamically inside web pages. There are different ways for dynamically inserting or executing several JavaScript inside your web page. This is a function for appending a child to the parent element which is here body.
function loadinlinescript(){
Dynamic Javascript Insertion Fortunately, dynamic insertion of CSS or Javascript is relatively painless. var headID = document.getElementsByTagName("head")[0]; It's really that simple. headID gets the <head> element of the page. Next we create a new 'script' element, assign it a text/javascript type, and then the url of the script (which can be anywhere on the net since it's basically a javascript include). Finally we append the new element to our head section where it is automatically loaded. If you're loading an external javascript and you need to know when the script has loaded you can simply use .onload=yourfunction; to set up the onload handler. Here's an example. var headID = document.getElementsByTagName("head")[0]; Now when this script has been loaded the .onload event will call a function named scriptLoaded(). Adding Javascript Through AjaxOn your own site and domain you can use dynamic script loading to keep the size of your javascripts small and serve only what the user actually needs, as the scripts are needed. You should of course use AJAX ( responseText ) to retrieve JSON formated data and Javascripts off your own site instead of dynamic <script> attachment. Here's an example of an ajax object which will load a javascript file then pass it through the eval command (which is basically just the javascript compiler). After this is done the functions and variables will be available to the other functions in your web page. function ajaxObject(url, callbackFunction) { It's important to note that we don't need a server-side script here. When we create our ajaxObject we pass it the url of a javascript file which is the exact same as any file you'd set up as an external <script>. The ajaxObject will retrieve the file then pass it to attachScript which then passes the file through eval which executes it exactly as if it were being loaded in via the <script> tag. You can call a script if you want and have php or some other server-side scripting language generate custom, dynamic javascript on the fly, but you can also call a static javascript file as well. Trackback(0)
Comments (4)
![]() توکلی جان دستت درد نکنه
written by Casitecenter, March 05, 2008
توکلی جان دستت درد نکنه
واقعا افتخار بايد کرد به جوان هايی مثل شما بی نظير ترين اجکس واسه جملا همينه که نوشتی منتظر نسخه های جديدش هستيم Awesome - but I have an issue
written by Fahd, March 07, 2008
Everything is working just fine EXCEPT the functions in my JS file are not being recognized, but code that isn't in a function is fine. Firebug keeps saying its "not defined". It seems like its just not being eval-ed, or maybe its something else. Any solution?
Event handler written by raj, June 30, 2008 Write comment
Copyright 2007. All Rights Reserved. |
|
| Last Updated ( Friday, 28 September 2007 ) |
| < Prev |
|---|









