Getting an external content with javascript.

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

    #1

    Getting an external content with javascript.

    Can i get the content of an URL and put it in a variable using javascript?
    I want js execute different operations varying a file that is on internet.
    Thanks, regards.


  • kaeli

    #2
    Re: Getting an external content with javascript.

    In article <cd3nfj$kq9$1@l acerta.tiscalin et.it>,
    vainaQUESTOtogl ilo@quipo.itttttt enlightened us with...[color=blue]
    > Can i get the content of an URL and put it in a variable using javascript?
    > I want js execute different operations varying a file that is on internet.
    > Thanks, regards.
    >
    >
    >[/color]

    Not in plain, ordinary, cross-browser, client-side javascript for
    general internet use.

    If your situation is different than the above, let us know.
    ActiveX, COM, applets, and other ways exist to do this, but none of them
    work on every browser for every platform and thus are not suitable for
    general internet use.

    Server-side scripting could do this with no problems.

    --
    --
    ~kaeli~
    A lot of money is tainted - It taint yours and it taint mine.



    Comment

    • Grant Wagner

      #3
      Re: Getting an external content with javascript.

      Lorenzo Vaina wrote:
      [color=blue]
      > Can i get the content of an URL and put it in a variable using javascript?
      > I want js execute different operations varying a file that is on internet.
      > Thanks, regards.[/color]

      No, client-side Javascript can neither read nor write files on a Web server
      served from another domain.

      Yes, client-side Javascript can be made to "read" files from a Web server
      served from the same domain that the Javascript was served from, but it can
      not permanently write any changes back to the server without help from
      executable code running on the server (ASP, C, JSP, Perl, PHP, etc).

      --
      Grant Wagner <gwagner@agrico reunited.com>
      comp.lang.javas cript FAQ - http://jibbering.com/faq


      Comment

      • Jimmy Cerra

        #4
        Re: Getting an external content with javascript.

        > Can i get the content of an URL and put it in a variable using javascript?[color=blue]
        > I want js execute different operations varying a file that is on internet.[/color]

        In general, no. In the context of a particular browser, yes with
        caveats. I wrote about two ways with Internet Explorer 5+ in my
        journal [1]. With MSIE 5+, you can also use the download DHTML
        behavior (although there are memory leaks using it sometimes).
        Mozilla's JavaScript engine also mimics some parts of IE's interface
        with the XMLHttpRequest object [3].

        Hope that helps,

        --
        Jimmy Cerra

        [1] http://slashdot.org/~Quantum%20Jim/journal/72680/

        [2] http://msdn.microsoft.com/workshop/a...s/download.asp

        [3] http://jibbering.com/2002/4/httprequest.html

        Comment

        • Lorenzo Vaina

          #5
          Re: Getting an external content with javascript.

          > No, client-side Javascript can neither read nor write files on a Web
          server[color=blue]
          > served from another domain.[/color]

          Ok.
          [color=blue]
          > Yes, client-side Javascript can be made to "read" files from a Web server
          > served from the same domain that the Javascript was served from,[/color]

          How please, if you are so patient?
          [color=blue]
          > but it can
          > not permanently write any changes back to the server without help from
          > executable code running on the server (ASP, C, JSP, Perl, PHP, etc).
          >[/color]

          Obviously.

          Thanks, regards.


          Comment

          • Grant Wagner

            #6
            Re: Getting an external content with javascript.

            Lorenzo Vaina wrote:
            [color=blue][color=green]
            > > No, client-side Javascript can neither read nor write files on a Web[/color]
            > server[color=green]
            > > served from another domain.[/color]
            >
            > Ok.
            >[color=green]
            > > Yes, client-side Javascript can be made to "read" files from a Web server
            > > served from the same domain that the Javascript was served from,[/color]
            >
            > How please, if you are so patient?[/color]

            Using the XML HTTP Request object available in IE or Gecko-based browsers.
            Information on this is available at:
            <url: http://jibbering.com/2002/4/httprequest.html />

            Or you can retrieve the content from the server into a hidden <iframe> and read
            it from there:

            <body onload="test(); ">
            <iframe id="contentId" name="contentNa me" style="display: none;"
            src="test.txt"> </iframe>
            <script type="text/javascript">
            function test() {
            var iframe;
            if (document.getEl ementById && !document.frame s) {
            iframe = document.getEle mentById('conte ntId');
            if (iframe &&
            iframe.contentD ocument &&
            iframe.contentD ocument.body &&
            typeof iframe.contentD ocument.body.in nerHTML == 'string') {

            alert('using getElementById: ' +
            iframe.contentD ocument.body.in nerHTML);
            }
            } else if (document.frame s) {
            iframe = document.frames['contentName'];
            if (iframe &&
            iframe.document &&
            iframe.document .body &&
            typeof iframe.document .body.innerHTML == 'string') {

            alert('using document.frames : ' + iframe.document .body.innerHTML );
            }
            } else {
            alert('Browser does not support examining the content of an iframe');
            }
            }
            </script>

            --
            Grant Wagner <gwagner@agrico reunited.com>
            comp.lang.javas cript FAQ - http://jibbering.com/faq


            Comment

            • Lorenzo Vaina

              #7
              Re: Getting an external content with javascript.

              Thanks for your reply.
              [color=blue]
              > Or you can retrieve the content from the server into a hidden <iframe> and[/color]
              read[color=blue]
              > it from there:[/color]

              This metod appears more compatible; pharaps i could retrieve and read the
              url content in a window too; i will study how make the text not readeable by
              humans.


              Comment

              • Lorenzo Vaina

                #8
                Re: Getting an external content with javascript.

                Thanks for your reply.


                Comment

                Working...