Solved

javascript concurrency issue

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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 …
Suggested Courses

636 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