Solved

javascript concurrency issue

Posted on 2006-07-18
3
433 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now