Multithreading? Or the equivalent?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    Multithreading? Or the equivalent?

    Is there a way to achieve multithreading in JavaScript? I'm looking to
    fetch a page into a div while allowing the user to interact with another
    div. At some point the newly fetched page contents will be available to the
    div that the user is working in but I don't want to cause unnecessary
    blocking. I've thought of using frames (would prefer divs) plus timeouts
    and checking for when a load completes. Does anyone have an idea of how
    this could work?


  • Csaba Gabor

    #2
    Re: Multithreading? Or the equivalent?

    You can have a SCRIPT element
    <SCRIPT type='text/javascript' id=comm1></SCRIPT>
    and set the src attribute like so:
    document.getEle mentById('comm1 ') = 'dome.js'

    At the end of your dome.js file you should do a
    window.setTimeo ut() of whatever function you have in
    your original page waiting to be run.

    I don't know that I'd call this multithreading though.
    Also, you should figure out how you want to deal
    with the server not returning the .js file in time/at all.

    Csaba Gabor from Budapest

    "Mark" <Mark.Will@gems .gov.bc.ca> wrote in message
    news:40900092@o bsidian.gov.bc. ca...[color=blue]
    > Is there a way to achieve multithreading in JavaScript? I'm looking to
    > fetch a page into a div while allowing the user to interact with another
    > div. At some point the newly fetched page contents will be available to[/color]
    the[color=blue]
    > div that the user is working in but I don't want to cause unnecessary
    > blocking. I've thought of using frames (would prefer divs) plus timeouts
    > and checking for when a load completes. Does anyone have an idea of how
    > this could work?
    >
    >[/color]


    Comment

    • Brian Genisio

      #3
      Re: Multithreading? Or the equivalent?

      Mark wrote:
      [color=blue]
      > Is there a way to achieve multithreading in JavaScript? I'm looking to
      > fetch a page into a div while allowing the user to interact with another
      > div. At some point the newly fetched page contents will be available to the
      > div that the user is working in but I don't want to cause unnecessary
      > blocking. I've thought of using frames (would prefer divs) plus timeouts
      > and checking for when a load completes. Does anyone have an idea of how
      > this could work?
      >
      >[/color]

      Using setTimeout and setInterval, you can achieve a "multi-threaded"
      effect. You need to make sure that nothing really blocks though.
      You can use setInterval to do a periodic check if the data is ready, and
      unschedule the interval once the task has completed.

      You can use IFRAMES to load data in the background. You can set the
      visibility to hidden, and the user will never see the page loading.
      Once the page is loaded, you can set the visibility to show, and you
      should be able to achieve what you want.

      B

      Comment

      • Martin Honnen

        #4
        Re: Multithreading? Or the equivalent?



        Mark wrote:
        [color=blue]
        > Is there a way to achieve multithreading in JavaScript? I'm looking to
        > fetch a page into a div while allowing the user to interact with another
        > div. At some point the newly fetched page contents will be available to the
        > div that the user is working in but I don't want to cause unnecessary
        > blocking. I've thought of using frames (would prefer divs) plus timeouts
        > and checking for when a load completes. Does anyone have an idea of how
        > this could work?[/color]

        Client side JavaScript so far doesn't have any threading constructs but
        it is event based and you can start loading an image for instance with
        var img = new Image();
        img.src = 'whatever.gif';
        which the browser usually then does in a thread of its own and if you
        have registered an onload event listener e.g
        var img = new Image();
        img.onload = function (evt) {
        alert(this.src + ' loaded.');
        };
        img.src = 'whatever.gif';
        then your event listener will be called once the image is loaded.

        Some browsers like IE5+/Win and Netscape 6/7 and Mozilla based browsers
        have special objects allowing you to make asynchronous HTTP requests and
        set up event listeners to handle the response:
        var httpRequest;
        if (typeof ActiveXObject != 'undefined') {
        httpRequest = new ActiveXObject(' Msxml2.XMLHTTP' );
        }
        else if (typeof XMLHttpRequest != 'undefined') {
        httpRequest = new XMLHttpRequest( );
        }
        if (httpRequest) {
        //open(httpReques tMethod, URL, async)
        httpRequest.ope n('GET', 'whatever.html' , true);
        httpRequest.onr eadystatechange = function () {
        if (httpRequest.re adyState == 4) {
        alert(httpReque st.responseText );
        }
        };
        httpRequest.sen d(null);
        }

        That being demonstrated you should first consider making a site that
        simply loads your page into an HTML iframe as that doesn't depend on
        scripting being supported and enabled and should give you all the
        advantages of interactivity and threaded request/response processing.
        --

        Martin Honnen


        Comment

        • Randy Webb

          #5
          Re: Multithreading? Or the equivalent?

          Csaba Gabor wrote:
          [color=blue]
          > You can have a SCRIPT element
          > <SCRIPT type='text/javascript' id=comm1></SCRIPT>
          > and set the src attribute like so:
          > document.getEle mentById('comm1 ') = 'dome.js'[/color]

          Assuming you meant document.getEle mentById('comm1 ').src instead, one
          should still be careful about attempting to change the src attribute of
          a script tag. It does not work in all browsers. It works (as in, it
          loads the .js file), in IE5.0, 5.5, 6.0 Windows, IE5 Mac and Opera
          7/Windows (at least version 7.23 does), but does *not* work in any other
          browser that I am aware of. If you know of a browser/OS combination
          where it works that is not listed, could you please let me know browser,
          revision, and OS?




          --
          Randy
          Chance Favors The Prepared Mind
          comp.lang.javas cript FAQ - http://jibbering.com/faq/

          Comment

          • Michael Winter

            #6
            Re: Multithreading? Or the equivalent?

            On Wed, 28 Apr 2004 22:00:59 +0200, Csaba Gabor <news@CsabaGabo r.com>
            wrote:
            [color=blue]
            > You can have a SCRIPT element
            > <SCRIPT type='text/javascript' id=comm1></SCRIPT>
            > and set the src attribute like so:
            > document.getEle mentById('comm1 ') = 'dome.js'
            >
            > At the end of your dome.js file you should do a
            > window.setTimeo ut() of whatever function you have in
            > your original page waiting to be run.
            >
            > I don't know that I'd call this multithreading though.
            > Also, you should figure out how you want to deal
            > with the server not returning the .js file in time/at all.[/color]

            In addition to what Randy said, you should be aware that the SCRIPT
            element doesn't support an id attribute under any version of HTML or
            XHTML. This has two implications:

            1) You can't write valid HTML of any kind (unless you use your own DTD)
            2) Strictly conforming browsers shouldn't return a SCRIPT element that has
            a matching id.

            Mike


            Please don't top-post.

            --
            Michael Winter
            M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

            Comment

            • Csaba Gabor

              #7
              Re: Multithreading? Or the equivalent?

              "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
              news:opr7b1lhem 5vklcq@news-text.blueyonder .co.uk...
              ....[color=blue]
              > In addition to what Randy said, you should be aware that the SCRIPT
              > element doesn't support an id attribute under any version of HTML or
              > XHTML. This has two implications:
              >
              > 1) You can't write valid HTML of any kind (unless you use your own DTD)
              > 2) Strictly conforming browsers shouldn't return a SCRIPT element that has
              > a matching id.[/color]

              What about setting .src on document.script s[#]?
              Will this also not work in Netscape, etc.?

              Csaba Gabor


              Comment

              • Michael Winter

                #8
                Re: Multithreading? Or the equivalent?

                On Sun, 2 May 2004 13:10:14 +0200, Csaba Gabor <news@CsabaGabo r.com> wrote:
                [color=blue]
                > "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
                > news:opr7b1lhem 5vklcq@news-text.blueyonder .co.uk...
                > ...[color=green]
                >> In addition to what Randy said, you should be aware that the SCRIPT
                >> element doesn't support an id attribute under any version of HTML or
                >> XHTML. This has two implications:
                >>
                >> 1) You can't write valid HTML of any kind (unless you use your own DTD)
                >> 2) Strictly conforming browsers shouldn't return a SCRIPT element that
                >> has a matching id.[/color]
                >
                > What about setting .src on document.script s[#]?
                > Will this also not work in Netscape, etc.?[/color]

                I shouldn't think so. The scripts collection is a Microsoft-ism, though
                Opera supports it too. The latest version of Mozilla (1.8a) doesn't, so I
                doubt Netscape will.

                There's nothing wrong with using getElementsByTa gName() to achieve the
                same effect, but Mozilla still isn't dynamic enough to load the new
                script. It won't be the only one.

                Mike

                --
                Michael Winter
                M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

                Comment

                • Randy Webb

                  #9
                  Re: Multithreading? Or the equivalent?

                  Michael Winter wrote:[color=blue]
                  > On Sun, 2 May 2004 13:10:14 +0200, Csaba Gabor <news@CsabaGabo r.com> wrote:
                  >[color=green]
                  >> "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
                  >> news:opr7b1lhem 5vklcq@news-text.blueyonder .co.uk...
                  >> ...
                  >>[color=darkred]
                  >>> In addition to what Randy said, you should be aware that the SCRIPT
                  >>> element doesn't support an id attribute under any version of HTML or
                  >>> XHTML. This has two implications:
                  >>>
                  >>> 1) You can't write valid HTML of any kind (unless you use your own DTD)
                  >>> 2) Strictly conforming browsers shouldn't return a SCRIPT element
                  >>> that has a matching id.[/color]
                  >>
                  >>
                  >> What about setting .src on document.script s[#]?
                  >> Will this also not work in Netscape, etc.?[/color]
                  >[/color]

                  IE and Opera7 only, at the moment.
                  [color=blue]
                  >
                  > I shouldn't think so. The scripts collection is a Microsoft-ism, though
                  > Opera supports it too. The latest version of Mozilla (1.8a) doesn't, so
                  > I doubt Netscape will.[/color]

                  No, Netscape/Mozilla don't support changing the .src attribute (It
                  changes it, just doesn't load the .js file).

                  [color=blue]
                  > There's nothing wrong with using getElementsByTa gName() to achieve the
                  > same effect, but Mozilla still isn't dynamic enough to load the new
                  > script. It won't be the only one.[/color]



                  Shows where it can/cant be dynamically loaded, and what method is used
                  to do it with. Mozilla can dynamically load a .js file via createElement.

                  While the chart does not show it (I removed it), dynamically loading can
                  be achieved (on a PC anyway) in NN4.xx by opening a layer, writing a
                  script tag to it, then closing it.


                  --
                  Randy
                  Chance Favors The Prepared Mind
                  comp.lang.javas cript FAQ - http://jibbering.com/faq/

                  Comment

                  Working...