Accessing an included source file as a string

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

    Accessing an included source file as a string

    I'm a javascript novice trying to write a client-side program that
    needs to examine the text of some included code.

    Suppose you have the following snippet in an html document:

    <script id="included" src="inc.js" type="text/javascript"></script>

    I want to be able to write a function along the following lines:

    function inspect() {
    var s = document.findBy ElementId("incl uded");
    var includedCodeAsS tring = ... ; //<------- s.nodeValue,
    s.innerHTML???
    //analyze includedCodeAsS tring
    }

    That is, I want to be able to access the file inc.js which should be
    have been downloaded by the browser. I've tried things like s.innerHTML
    without luck.

    My current solution to this is to use AJAX (XMLHttpRequest ) to GET
    inc.js from the server ... but this feels wrong.

    Thanks for any help you can provide.
    -Nik

  • web.dev

    #2
    Re: Accessing an included source file as a string


    niks wrote:
    <script id="included" src="inc.js" type="text/javascript"></script>
    The script element does not have an id attribute.
    var s = document.findBy ElementId("incl uded");
    The method name is incorrect, it should be the following:

    var s = document.getEle mentById("inclu ded");

    But, the script element does not have an id attribute as said above,
    therefore your statement will still not do what you wanted to do.
    >
    My current solution to this is to use AJAX (XMLHttpRequest ) to GET
    inc.js from the server ... but this feels wrong.
    Your solution using AJAX sounds reasonable.

    Comment

    • RobG

      #3
      Re: Accessing an included source file as a string


      niks wrote:
      I'm a javascript novice trying to write a client-side program that
      needs to examine the text of some included code.
      >
      Suppose you have the following snippet in an html document:
      >
      <script id="included" src="inc.js" type="text/javascript"></script>
      >
      I want to be able to write a function along the following lines:
      >
      function inspect() {
      var s = document.findBy ElementId("incl uded");
      var includedCodeAsS tring = ... ; //<------- s.nodeValue,
      s.innerHTML???
      //analyze includedCodeAsS tring
      }
      >
      That is, I want to be able to access the file inc.js which should be
      have been downloaded by the browser. I've tried things like s.innerHTML
      without luck.
      Probably because there is no innerHTML (there is actually no content at
      all). What you are trying to get is the content of the resource
      identified by the script element's src attribute.

      My current solution to this is to use AJAX (XMLHttpRequest ) to GET
      inc.js from the server ... but this feels wrong.
      That is probably the the best way, though it has all the usual AJAX
      caveats. You can try loading the script into a hidden iFrame element,
      then grab it from there, but I get security warnings from IE and it
      won't let me see the file (I tried a resonably complex bit of script).
      Firefox seems happy. You won't be able to do it if the script is from
      another domain.

      You will also get issues with the iFrame method where characters in the
      script are interpreted as HTML - particularly "<" being taken as the
      start of an opening tag. I would characterise it as extremely
      unreliable.

      I would have to question why you are downloading a script from your own
      server, then trying to process it using another script - are you trying
      to mess with someone eles's script?


      --
      Rob

      Comment

      • niks

        #4
        Re: Accessing an included source file as a string

        RobG wrote:
        My current solution to this is to use AJAX (XMLHttpRequest ) to GET
        inc.js from the server ... but this feels wrong.
        >
        That is probably the the best way, though it has all the usual AJAX
        caveats.
        That's too bad. It seems like an unnecessary round-trip to the server
        to fetch something that the browser should already have. Anyway ... I
        just wanted to make sure that I wasn't missing some obvious way of
        doing this without AJAX. Thanks very much for your help.

        Comment

        Working...