Why isn´t this possible?

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

    Why isn´t this possible?

    I´m trying to make an application with if ...something
    else if ... something else to dinamically change a src script,
    can't this be done ? and if so why not ?
    Thanks in advance , Oscar Monteiro
    <span id="script">
    <script type="text/javascript" >
    script.innerHTM L="script src='a.js' ";
    </script>



  • Stewart Gordon

    #2
    Re: Why isn´t this possible?

    Oscar Monteiro wrote:
    [color=blue]
    > I´m trying to make an application with if ...something
    > else if ... something else to dinamically change a src script,
    > can't this be done ? and if so why not ?[/color]

    JavaScript defined in a script block is executed as and when the HTML
    page is initially interpreted. This includes reading in any functions
    defined in the script block. If you try programmaticall y changing the
    content of a script block, I don't think the behaviour is defined.

    However, a possibility is to do something like

    <script type="text/javascript"><!--

    document.write( '<script type="text/javascript" src="a.js"><\/script>');

    // --></script>

    Note the backslash, which makes extra sure that the closing script tag
    that you write out isn't interpreted as the end of this code.

    The exact time at which the included script is processed in relation to
    the script that includes it isn't well defined. You'll need to end this
    script block and start another before you can use functions defined in
    the .js file.
    [color=blue]
    > <span id="script">
    > <script type="text/javascript" >
    > script.innerHTM L="script src='a.js' ";
    > </script>[/color]

    If this worked, you would end up with

    <span id="script">scr ipt src='a.js' </span>

    which obviously isn't what you intended. Before you count the fact that
    you have nothing with a _name_ (as opposed to id) of script.

    Stewart.

    Comment

    • VK

      #3
      Re: Why isn´t this possible?

      IMHO the question was:

      Why JavaScript (at least in its ISO-blessed ECMA version) still refuses to
      add standard precompiler instructions like
      #if
      #elseif
      #const
      etc.

      The sincere answer would be: "God knows: why!".
      Maybe they just like to see us being stoke in "90's forever" with these ugly
      ....
      else (document.write ("<scr"+"ipt src='some.js'>" +"</scr"+"ipt>");)
      ....


      Comment

      • Michael Winter

        #4
        Re: Why isn´t this possible?

        On Thu, 21 Oct 2004 17:00:05 +0100, Stewart Gordon <smjg_1998@yaho o.com>
        wrote:

        [snip]
        [color=blue][color=green]
        >> <span id="script">
        >> <script type="text/javascript" >
        >> script.innerHTM L="script src='a.js' ";[/color][/color]

        [snip]
        [color=blue]
        > Before you count the fact that you have nothing with a _name_ (as
        > opposed to id) of script.[/color]

        Even so, an identifier matching the name of an element does not, by
        itself, access that element. The appropriate collection, whether it be
        document.forms, .links, .images, should also be used.

        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        • Michael Winter

          #5
          Re: Why isn´t this possible?

          On Thu, 21 Oct 2004 19:13:26 +0200, VK <schools_ring@y ahoo.com> wrote:

          [snip]
          [color=blue]
          > Why JavaScript (at least in its ISO-blessed ECMA version) still refuses
          > to add standard precompiler instructions like
          > #if
          > #elseif
          > #const
          > etc.
          >
          > The sincere answer would be: "God knows: why!".[/color]

          ECMAScript makes no assumptions as to the environment in which it runs.
          Such extensions would be just that: extensions. Besides, you can simulate
          conditional compilation with standard language constructs, which is
          probably why it isn't an extension to an existing implementation.

          By the way, you never know what might be added in future as ECMAScript has
          a large list of reserved keywords (most from Java):

          abstract enum int short
          boolean export interface static
          byte extends long super
          char final native synchronized
          class float package throws
          const goto private transient
          debugger implements protected volatile
          double import public

          [snip]
          [color=blue]
          > else (document.write ("<scr"+"ipt src='some.js'>" +"</scr"+"ipt>");)[/color]

          At the very least, that last part should be changed to:

          '<\/src' + 'ipt>'

          Although there is no current evidence to suggest that browsers do
          interpret a </ sequence to mean the end of a SCRIPT element, for the sake
          of an extra character, you shouldn't take that risk.

          Mike

          --
          Michael Winter
          Replace ".invalid" with ".uk" to reply by e-mail.

          Comment

          • kaeli

            #6
            Re: Why isn´t this possible?

            In article <4177d110$0$180 50$a729d347@new s.telepac.pt>, offm@hotmail.co m
            enlightened us with...[color=blue]
            > I=3Fm trying to make an application with if ...something
            > else if ... something else to dinamically change a src script,
            > can't this be done ? and if so why not ?
            > Thanks in advance , Oscar Monteiro
            > <span id="script">
            > <script type="text/javascript" >
            > script.innerHTM L="script src='a.js' ";
            > </script>
            >
            >
            >
            >[/color]

            See here for an example of dynamic script elements. DOM browsers only.


            --
            --
            ~kaeli~
            Murphy's Law #2000: If enough data is collected, anything
            may be proven by statistical methods.



            Comment

            • Randy Webb

              #7
              Re: Why isn´t this possible?

              Oscar Monteiro wrote:
              [color=blue]
              > I´m trying to make an application with if ...something
              > else if ... something else to dinamically change a src script,
              > can't this be done ? and if so why not ?[/color]

              Yes, you can dynamically change the src attribute of a script tag. But
              only in IE. In any other browser you have to create a new script tag and
              load the src file. Do a search of the c.l.j archives for "dynamicall y
              loading .js file" and you can find threads where, among other people, I
              have talked about doing just what you want to do.

              --
              Randy
              comp.lang.javas cript FAQ - http://jibbering.com/faq

              Comment

              • VK

                #8
                Re: Why isn´t this possible?

                [color=blue]
                > ECMAScript makes no assumptions as to the environment in which it runs.[/color]

                But it should. It never pretended on the Java's WORA slogan, so this
                pharossy is escaping my understanding.
                [color=blue]
                > Besides, you can simulate conditional compilation with standard language[/color]
                constructs,

                Anything can be done in more than one way. Just look at NN4/IE4 - compatible
                historic scripts: A monument to the human creativity non-surrender spirit.
                But why to keep it this way?
                [color=blue]
                > By the way, you never know what might be added in future as ECMAScript >[/color]

                Why it is not done now?
                "God knows: why" - my answer remains the most profound one.
                [color=blue]
                > At the very least, that last part should be changed to:
                >
                > '<\/src' + 'ipt>'[/color]

                .... and so on: thank you for an illustration of how ugly the current
                situation is with the pseudo-precompiled instructions.


                Comment

                • Michael Winter

                  #9
                  Re: Why isn´t this possible?

                  On Fri, 22 Oct 2004 10:56:26 +0200, VK <schools_ring@y ahoo.com> wrote:
                  [color=blue][color=green]
                  >> ECMAScript makes no assumptions as to the environment in which it runs.[/color]
                  >
                  > But it should.[/color]

                  No, it shouldn't. It is a scripting language with applications outside of
                  browsers, so there cannot be any assumptions about the environment.
                  [color=blue]
                  > It never pretended on the Java's WORA slogan, so this pharossy is
                  > escaping my understanding.[/color]

                  Pharossy?

                  [snip]
                  [color=blue][color=green]
                  >> By the way, you never know what might be added in future as ECMAScript >[/color]
                  >
                  > Why it is not done now?[/color]

                  Because there's no need. If the precondition can be evaluated on the
                  server, it can insert the required script. If not, a script replace
                  functions at runtime to adapt to its environment. That, in fact, provides
                  more functionality. Preprocessing instructions are executed at parse-time,
                  which may not be an appropriate moment to evaluate the environment, so
                  again, I fail to see the need.

                  [snip]
                  [color=blue][color=green]
                  >> At the very least, that last part should be changed to:
                  >>
                  >> '<\/src' + 'ipt>'[/color]
                  >
                  > ... and so on: thank you for an illustration of how ugly the current
                  > situation is with the pseudo-precompiled instructions.[/color]

                  The correction had nothing to do with the "current situation", as you see
                  it. Escaping the backslash should be done with *every* closing tag, not
                  just closing SCRIPT tags. Perhaps you should read
                  <URL:http://www.w3.org/TR/html4/appendix/notes.html#h-B.3.2>. Of course,
                  this only applies within a HTML document. If the script is in an external
                  file, there's no problem.

                  Mike

                  --
                  Michael Winter
                  Replace ".invalid" with ".uk" to reply by e-mail.

                  Comment

                  • Stewart Gordon

                    #10
                    Re: Why isn´t this possible?

                    VK wrote:[color=blue]
                    > IMHO the question was:
                    >
                    > Why JavaScript (at least in its ISO-blessed ECMA version) still refuses to
                    > add standard precompiler instructions like
                    > #if
                    > #elseif
                    > #const
                    > etc.[/color]

                    Because JavaScript has no standard precompiler.

                    And because some consider the preprocessor to be an abomination. This
                    is part of the design of D:


                    (scroll down to 'Features to Drop')

                    But then, I'm not sure what this is to do with anything....
                    [color=blue]
                    > The sincere answer would be: "God knows: why!".
                    > Maybe they just like to see us being stoke in "90's forever" with these ugly
                    > ...
                    > else (document.write ("<scr"+"ipt src='some.js'>" +"</scr"+"ipt>");)
                    > ...[/color]

                    That doesn't look like valid JS syntax.

                    Stewart.

                    Comment

                    Working...