Solved

javascript concurrency issue

Posted on 2006-07-18
3
436 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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

789 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