SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jul 2011
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post file js interaction file .js ?

    help me!

    im have 2 file .js
    file 1.js code:
    document.write("<script src=2.js></script>")
    document.write(f)

    file 2.js code:
    var f="hello"

    im want run 1.js get variable of file 2.js by 3.html but don't run!

    code 3.html

    <script src=1.js></script>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    When the document.write code executes, that updates the DOM with the command to bring in 2.js, but the 2.js coe is not yet available. It has to be downloaded and run before you can make use of what's in there.

    You could wait for a long period of time until it's available, or you can write some code to monitor things so that once the loaded code is available, you can do something.

    There are a wide-range of javascript loaders that can do the job for you, or you can use something like this:

    Code javascript:
    function waitFor(trigger, callback) {
        if (trigger()) {
            callback();
        } else {
            setTimeout(function () {
                waitFor(trigger, callback);
            }, 100);
        }
    }

    which you could call with:

    Code javascript:
    waitFor(function () {
        return typeof(f) !== 'undefined';
    }, function () {
        document.write(f);
    });
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Jul 2011
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •