HI All
I have come across a requirement of
Its pretty Simple Code
Now I am happy the file is loaded and i get alert saying
boom we got a error @alertload();
So what can we do now?
we know every DOM has a ONLOAD event so lets try this
Yahoo..... Google chrome Showed the alert Now As most of the users still use IE,we need a test on IE
LET THE TEST BEGIN
Now the Above code will work in all the Browsers. Happy Coding :)
Dynamically loading the Javascript file On request.
Its pretty Simple Code
- <script type="text/javascript">
- function loadScript() {
- var script = document.createElement("script");
- script.type = "text/javascript";
- script.src = 'JScriptDynamic.js';
- document.body.appendChild(script);
- }
- window.onload = loadScript;
- </script>
- $(function(){
- alert('FileLoaded');
- })
Now I am happy the file is loaded and i get alert saying
FileLoadednow lets try to change the code little bit
- <script type="text/javascript">
- function loadScript() {
- var script = document.createElement("script");
- script.type = "text/javascript";
- script.src = 'JScriptDynamic.js';
- document.body.appendChild(script);
- alertload();
- }
- window.onload = loadScript;
- </script>
- function alertload()
- {
- alert(FileLoaded);
- }
boom we got a error @alertload();
Microsoft JScript runtime error: Object expectedLets Dig...Why Does this happen? Ok:) no digging and ploughing its pretty simple logic
JScriptDynamic.jsnot yet loaded
So what can we do now?
we know every DOM has a ONLOAD event so lets try this
- <script type="text/javascript">
- function loadScript() {
- var script = document.createElement("script");
- script.type = "text/javascript";
- script.src = 'JScriptDynamic.js';
- document.body.appendChild(script);
- script.onload = function () {
- alertme();
- }
- }
- window.onload = loadScript;
- </script>
- function alertload()
- {
- alert(FileLoaded);
- }
Yahoo..... Google chrome Showed the alert Now As most of the users still use IE,we need a test on IE
LET THE TEST BEGIN
its been 2 MIN !!!!!!!!!!!!!!!!!!!!!!!!!!!!! Where is my alert BOX?don't worry IE Doesn't support onload for script element so we have a work around
- function loadScript( ) {
- var script = document.createElement("script");
- script.type = "text/javascript";
- script.src = "JScriptDynamic.js";
- document.body.appendChild(script);
- var browser = navigator.appName;
- if (browser = "Microsoft Internet Explorer") {
- ieLoadBugFix(script, function () {
- //alert('IE');
- alertload();
- });
- }
- else {
- script.onload = function () {
- //alert('nonIE');
- alertload()
- }
- }
- }
- function ieLoadBugFix(scriptElement, callback) {
- if (scriptElement.readyState == 'loaded' || scriptElement.readyState == 'completed') {
- callback();
- } else {
- setTimeout(function () { ieLoadBugFix(scriptElement, callback); }, 100);
- }
- }
Now the Above code will work in all the Browsers. Happy Coding :)
0 comments:
Post a Comment