Solved

javascript concurrency issue

Posted on 2006-07-18
3
438 Views
Last Modified: 2013-11-19
i am having a problem with javascript continuing to process before a function is completed.
what i am trying to do is call the following function to include a .js file so that its functions will be available to my program.

function include_dom(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}

after i call the include_dom function to access a .js file, i immediately start trying to use the .js file's functions. the problem i am having is that the browser is continuing to process the code before the .js file has been included and is running into problems trying to call a function that has not been loaded in the .js file yet.

I need a  way to tell the browser to stop executing code until the include_dom function returns and the .js file has been added.

An example of my code is as follows:

include_dom('some_script.js');
some_scripts_function();
0
Comment
Question by:cali_oo7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 30

Expert Comment

by:third
ID: 17130320
try to set a delay, like,

include_dom('some_script.js');
window.setTimeout('some_scripts_function()', 1);

0
 
LVL 30

Assisted Solution

by:third
third earned 75 total points
ID: 17130330
try this,

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Author: Third Santor</title>
<script>
function include_dom(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}

include_dom('some_script.js');
window.setTimeout('some_scripts_function()', 1);
</script>
</head>
<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">
</body>
</html>


some_script.js
------------------------------
function some_scripts_function(){
  alert('test!');
}
0
 
LVL 2

Accepted Solution

by:
zeroreality earned 50 total points
ID: 17131799
the problem is that you need to wait for the file to be downloaded and parsed.

try this:
function include_dom(script_filename,script_action) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    if (navigator.appName.indexOf("Opera") >= 0) {
        script_action();
    } else if (navigator.appName.indexOf("Microsoft") >= 0) {
        js.onreadystatechange = function() {
            if (this.readyState == "loaded") { script_action() };
        };
    } else {
        js.onload = script_action;
    };
    html_doc.appendChild(js);
    return false;
}

the "script_action" parameter is a function that is fired when the newly added JS file is fully loaded.
Opera will load it before continuing to process the code.
IE6 will fire the onreadystatechanged event while loading, the readyState property becomes "loaded" when its done.
Firefox will fire the onload event after loading the file.
Safari I believe will also fire onreadystatechanged, but the value is "complete" instead of "loaded".  I don't have a Mac to test that though.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logiā€¦
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question