Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

javascript concurrency issue

Posted on 2006-07-18
3
Medium Priority
?
440 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 300 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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

722 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