Loading ...
Sorry, an error occurred while loading the content.
 

Re: Create script element

Expand Messages
  • Domenico Strazzullo
    it got truncated, here it is. Can anyone confirm that the following works (or doesn t work)?: var s = svgdoc.createElementNS(svgNS, script );
    Message 1 of 9 , Nov 27, 2007
      it got truncated, here it is.

      Can anyone confirm that the following works (or doesn't work)?:

      var s = svgdoc.createElementNS(svgNS,"script");
      s.setAttributeNS(xlinkNS,"xlink:href","xxxx.es");
      svgRoot.appendChild(s);

      After loading and parsing an external file the script linked to that
      file is dumb. Same if the script is inline. Then I tried the above with
      no luck. Can anyone suggest an alternate method, other than hardcoding
      the script tag in the first file(the script filename is not necessarily
      known in advance)?

      Thank you,

      Domenico


      --- In svg-developers@yahoogroups.com, "Domenico Strazzullo" <nst@...>
      wrote:
      >
      > Hi,
      >
      > Can anyone confirm that the following works (or doesn
      >
    • ddailey
      Hi Nico, What a fun problem! I fiddled around with it just enough to confirm what seems like odd behavior in the browsers: The enclosed code (with a simple .js
      Message 2 of 9 , Nov 27, 2007
        Hi Nico,

        What a fun problem!

        I fiddled around with it just enough to confirm what seems like odd behavior in the browsers:

        The enclosed code (with a simple .js file of some sort added on ) shows very different behavior in Opera 9.5alpha, FF1.5 and IE
        with FF able to run the code from the <script> built through DOM. FF behaves the way I would expect it to.

        I am not sure where within DOM one would expect the text of a .js (or .es) file to actually appear, which is why I was experimenting with various DOM explorations here. If the commented out code (both SVG and JavaScript) is commented back in then all three browsers see three script tags, but only in the first case, when the code is provided in-line, can I figure out how to interrogate the source code programmatically. Only in FF is the script, thusly constructed, executable.

        Since FF works the way I expect it should, it is probably the one which is buggy. Erik Dahlstrom has pointed out that some of the CDATA hoopla that I typically use is more extravagant than it needs to be, so in this context my extraneous hoopla could prove problematic since it does pop up as the nodeValue of the script that is commented out. Erik and maybe Martin H. have also mentioned the use of href.baseVal in setting xlinks, but this is on my list of things to ask about sometime, so am not sure why or when one would do that.

        cheers,
        David
        -------------------------
        <svg xmlns="http://www.w3.org/2000/svg" width="100%"
        xmlns:xlink="http://www.w3.org/1999/xlink" onload="start()"
        viewBox="0 0 100 100">
        <script><![CDATA[
        svgRoot=document.documentElement
        svgNS="http://www.w3.org/2000/svg"
        xlinkNS="http://www.w3.org/1999/xlink"
        svgRoot.setAttribute("onclick","runit()")
        function start(){
        var s = document.createElementNS(svgNS,"script");
        svgRoot.appendChild(s);
        s.setAttributeNS(xlinkNS,"xlink:href","xxxx.js");
        SCR=document.getElementsByTagName("script")
        alert(SCR.length+SCR.item(0).nodeName+SCR.item(0).nodeName+SCR.item(0).firstChild.nodeValue)
        alert(SCR.length+SCR.item(1).nodeName)
        alert(SCR.item(1).getAttribute("xlink:href"))
        //alert(SCR.item(2).nodeName)
        //alert(SCR.item(2).getAttribute("xlink:href"))
        runit()
        }
        //]]></script>
        <!--<script xlink:href="xxxx.js"></script>-->

        <rect x="0" y="0" id="R" height="100" width="100" fill="red" />

        </svg>


        ----- Original Message -----
        From: Domenico Strazzullo
        To: svg-developers@yahoogroups.com
        Sent: Tuesday, November 27, 2007 5:18 PM
        Subject: [svg-developers] Re: Create script element


        it got truncated, here it is.

        Can anyone confirm that the following works (or doesn't work)?:

        var s = svgdoc.createElementNS(svgNS,"script");
        s.setAttributeNS(xlinkNS,"xlink:href","xxxx.es");
        svgRoot.appendChild(s);

        After loading and parsing an external file the script linked to that
        file is dumb. Same if the script is inline. Then I tried the above with
        no luck. Can anyone suggest an alternate method, other than hardcoding
        the script tag in the first file(the script filename is not necessarily
        known in advance)?

        Thank you,

        Domenico

        --- In svg-developers@yahoogroups.com, "Domenico Strazzullo" <nst@...>
        wrote:
        >
        > Hi,
        >
        > Can anyone confirm that the following works (or doesn
        >





        [Non-text portions of this message have been removed]
      • Erik Dahlström
        On Wed, 28 Nov 2007 04:11:41 +0100, ddailey ... Ok, that is contrary to my experiments :) See below for source of that. ... This is
        Message 3 of 9 , Nov 28, 2007
          On Wed, 28 Nov 2007 04:11:41 +0100, ddailey <ddailey@...>
          wrote:

          > Hi Nico,
          >
          > What a fun problem!
          >
          > I fiddled around with it just enough to confirm what seems like odd
          > behavior in the browsers:
          >
          > The enclosed code (with a simple .js file of some sort added on ) shows
          > very different behavior in Opera 9.5alpha, FF1.5 and IE
          > with FF able to run the code from the <script> built through DOM. FF
          > behaves the way I would expect it to.

          Ok, that is contrary to my experiments :)
          See below for source of that.

          > I am not sure where within DOM one would expect the text of a .js (or
          > .es) file to actually appear, which is why I was experimenting with
          > various DOM explorations here. If the commented out code (both SVG and
          > JavaScript) is commented back in then all three browsers see three
          > script tags, but only in the first case, when the code is provided
          > in-line, can I figure out how to interrogate the source code
          > programmatically. Only in FF is the script, thusly constructed,
          > executable.

          This is another problem, no? You're looking for a way of getting the text
          content of a script element that has external content?

          > Since FF works the way I expect it should, it is probably the one which
          > is buggy. Erik Dahlstrom has pointed out that some of the CDATA hoopla
          > that I typically use is more extravagant than it needs to be, so in this
          > context my extraneous hoopla could prove problematic since it does pop
          > up as the nodeValue of the script that is commented out. Erik and maybe
          > Martin H. have also mentioned the use of href.baseVal in setting xlinks,
          > but this is on my list of things to ask about sometime, so am not sure
          > why or when one would do that.
          >
          > cheers,
          > David
          > -------------------------
          > <svg xmlns="http://www.w3.org/2000/svg" width="100%"
          > xmlns:xlink="http://www.w3.org/1999/xlink" onload="start()"
          > viewBox="0 0 100 100">
          > <script><![CDATA[
          > svgRoot=document.documentElement
          > svgNS="http://www.w3.org/2000/svg"
          > xlinkNS="http://www.w3.org/1999/xlink"
          > svgRoot.setAttribute("onclick","runit()")
          > function start(){
          > var s = document.createElementNS(svgNS,"script");
          > svgRoot.appendChild(s);
          > s.setAttributeNS(xlinkNS,"xlink:href","xxxx.js");
          > SCR=document.getElementsByTagName("script")
          > alert(SCR.length+SCR.item(0).nodeName+SCR.item(0).nodeName+SCR.item(0).firstChild.nodeValue)
          > alert(SCR.length+SCR.item(1).nodeName)
          > alert(SCR.item(1).getAttribute("xlink:href"))

          Replacing the line above with:

          alert(SCR.item(1).getAttributeNS(xlinkNS, "xlink:href"))

          gives me the correct xlink:href back in Opera as well.
          The question is whether this is correct in Firefox or not, is the name
          given to getAttribute a localName or a qualifiedName? The DOM specs are
          not very clear about that subject. And the same thing goes for
          setAttribute. Now logged as bug 300509 in Opera's bugtracker.

          > //alert(SCR.item(2).nodeName)
          > //alert(SCR.item(2).getAttribute("xlink:href"))
          > runit()
          > }
          > //]]></script>
          > <!--<script xlink:href="xxxx.js"></script>-->
          >
          > <rect x="0" y="0" id="R" height="100" width="100" fill="red" />
          >
          > </svg>

          So, here is the simple variant that I made up which works fine in the
          browsers I tested:

          <svg xmlns="http://www.w3.org/2000/svg"
          xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 1024 768"
          onload="init()">
          <script>
          var svgNS="http://www.w3.org/2000/svg";
          var xlinkNS="http://www.w3.org/1999/xlink";

          function init()
          {
          var s = document.createElementNS(svgNS,"script");
          s.setAttributeNS(xlinkNS,"xlink:href","myscript.es");
          document.documentElement.appendChild(s);
          }

          </script>
          </svg>

          The file myscript.es:

          alert("hello");

          Cheers
          /Erik

          --
          http://my.opera.com/MacDev_ed/blog
        • Domenico Strazzullo
          David, Erik, Sorry for my post being a little unclear. svgdoc.createElementNS(svgNS, script ) works for Opera and Firefox. One question was if there s an
          Message 4 of 9 , Nov 28, 2007
            David, Erik,

            Sorry for my post being a little unclear.

            svgdoc.createElementNS(svgNS,"script")

            works for Opera and Firefox. One question was if there's an
            alternative that works for Safari and ASV as well.

            The other issues are:
            1) In an XMLHttpRequest context, where one file lambda.svg is
            downloaded by main.svg, the script(s) of lambda don't execute, if
            even they are in the DOM (normal?).
            2) The workaround for above: load lambda; fetch the scripts strings
            and the onload attribute string; create script nodes in main; invoke
            lambda's init function as the onload event doesn't fire in any
            implementation (normal?).

            This architecture theoretically works, only, in Firefox the whole
            thing doesn't work because lambda is not loaded. At this point I must
            say that this idea comes directly from my autozoom widget (BTW, Geoff
            Whale has made a nice extension to it) where Firefox wouldn't load
            images but at least it would load svg's. After some headaches I have
            the brilliant idea of checking autozoom online, untouched. Result:
            now version 2.0.0.10 doesn't load svg's anymore.

            Safari doesn't do it either (unless SVG carbon).

            Finally, only Opera is up to this type of evolution that I want to
            implement in my gui, unless some of you guys can suggest another
            portable method.

            Thanks.

            OT
            Some remarks come to my mind about the state of the situation. At
            this point I'm frustrated with the fact that my creativity that was
            stimulated by the advent of SVG must come to a halt now, and I'd
            rather take an overdose than being condemned to do the same thing
            over and over again.

            When I read "Why is SVG development hampered by resources while
            MozCorp is sitting with 70 million pounds in th bank? That is what I
            read in a report a couple of months ago. Kinda missing the point of
            donations, ain't it!"
            (http://weblogs.mozillazine.org/tor/archives/2007/03/svg_priorities_in
            _firefox_3.html), when I know that the (tiny) svg team has to
            constantly postpone their deadlines fault of finding more volunteers,
            I say this is wrong.

            Microsoft. I actually know the real reason why they don't implement
            svg natively. They don't know how to do it. To do a native svg
            implementation is apparently difficult and for that, one needs to
            have some smart cookies in his sleeve, and Bill doesn't have any. He
            probably doesn't want any, either. I'm sure they secretly evaluated
            the task in-house and they got discouraged. Silverlight was safer
            (because blah blah… mistakes… blah blah… comparison… blah blah). I
            mean, they are really scared; the world should have by now places
            where to put away whole corporations that have gone too far out with
            their psychotic episodes. The huge squads of drones that they managed
            to produce over time should also be cured in some way.

            To cut it short (trust me, I'm cutting it short!), SVG has been out
            for almost a decade and yet now we are in the paradoxal position of
            having to choke our creativity; we have to wait now, see? We are also
            living another paradox, rediscovering the joys of having to spend
            loads of time with forks, switches and workarounds like in the old
            days of html, or else wait until the next miracle. Opera? I wish they
            could get 50% of the market but how can they?

            Domenico



            --- In svg-developers@yahoogroups.com, Erik Dahlström <ed@...> wrote:
            >
            > On Wed, 28 Nov 2007 04:11:41 +0100, ddailey <ddailey@...>
            > wrote:
            >
            > > Hi Nico,
            > >
            > > What a fun problem!
            > >
            > > I fiddled around with it just enough to confirm what seems like
            odd
            > > behavior in the browsers:
            > >
            > > The enclosed code (with a simple .js file of some sort added on )
            shows
            > > very different behavior in Opera 9.5alpha, FF1.5 and IE
            > > with FF able to run the code from the <script> built through DOM.
            FF
            > > behaves the way I would expect it to.
            >
            > Ok, that is contrary to my experiments :)
            > See below for source of that.
            >
            > > I am not sure where within DOM one would expect the text of a .js
            (or
            > > .es) file to actually appear, which is why I was experimenting
            with
            > > various DOM explorations here. If the commented out code (both
            SVG and
            > > JavaScript) is commented back in then all three browsers see
            three
            > > script tags, but only in the first case, when the code is
            provided
            > > in-line, can I figure out how to interrogate the source code
            > > programmatically. Only in FF is the script, thusly constructed,
            > > executable.
            >
            > This is another problem, no? You're looking for a way of getting
            the text
            > content of a script element that has external content?
            >
            > > Since FF works the way I expect it should, it is probably the one
            which
            > > is buggy. Erik Dahlstrom has pointed out that some of the CDATA
            hoopla
            > > that I typically use is more extravagant than it needs to be, so
            in this
            > > context my extraneous hoopla could prove problematic since it
            does pop
            > > up as the nodeValue of the script that is commented out. Erik and
            maybe
            > > Martin H. have also mentioned the use of href.baseVal in setting
            xlinks,
            > > but this is on my list of things to ask about sometime, so am not
            sure
            > > why or when one would do that.
            > >
            > > cheers,
            > > David
            > > -------------------------
            > > <svg xmlns="http://www.w3.org/2000/svg" width="100%"
            > > xmlns:xlink="http://www.w3.org/1999/xlink" onload="start()"
            > > viewBox="0 0 100 100">
            > > <script><![CDATA[
            > > svgRoot=document.documentElement
            > > svgNS="http://www.w3.org/2000/svg"
            > > xlinkNS="http://www.w3.org/1999/xlink"
            > > svgRoot.setAttribute("onclick","runit()")
            > > function start(){
            > > var s = document.createElementNS(svgNS,"script");
            > > svgRoot.appendChild(s);
            > > s.setAttributeNS(xlinkNS,"xlink:href","xxxx.js");
            > > SCR=document.getElementsByTagName("script")
            > > alert(SCR.length+SCR.item(0).nodeName+SCR.item
            (0).nodeName+SCR.item(0).firstChild.nodeValue)
            > > alert(SCR.length+SCR.item(1).nodeName)
            > > alert(SCR.item(1).getAttribute("xlink:href"))
            >
            > Replacing the line above with:
            >
            > alert(SCR.item(1).getAttributeNS(xlinkNS, "xlink:href"))
            >
            > gives me the correct xlink:href back in Opera as well.
            > The question is whether this is correct in Firefox or not, is the
            name
            > given to getAttribute a localName or a qualifiedName? The DOM specs
            are
            > not very clear about that subject. And the same thing goes for
            > setAttribute. Now logged as bug 300509 in Opera's bugtracker.
            >
            > > //alert(SCR.item(2).nodeName)
            > > //alert(SCR.item(2).getAttribute("xlink:href"))
            > > runit()
            > > }
            > > //]]></script>
            > > <!--<script xlink:href="xxxx.js"></script>-->
            > >
            > > <rect x="0" y="0" id="R" height="100" width="100" fill="red" />
            > >
            > > </svg>
            >
            > So, here is the simple variant that I made up which works fine in
            the
            > browsers I tested:
            >
            > <svg xmlns="http://www.w3.org/2000/svg"
            > xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 1024 768"
            > onload="init()">
            > <script>
            > var svgNS="http://www.w3.org/2000/svg";
            > var xlinkNS="http://www.w3.org/1999/xlink";
            >
            > function init()
            > {
            > var s = document.createElementNS(svgNS,"script");
            > s.setAttributeNS(xlinkNS,"xlink:href","myscript.es");
            > document.documentElement.appendChild(s);
            > }
            >
            > </script>
            > </svg>
            >
            > The file myscript.es:
            >
            > alert("hello");
            >
            > Cheers
            > /Erik
            >
            > --
            > http://my.opera.com/MacDev_ed/blog
            >
          • David Dailey
            ... I also was unable to get it working in ASV, whence all my dabbling about with DOM. In IE/ASV I was able to confirm that the element had been
            Message 5 of 9 , Nov 29, 2007
              At 07:53 PM 11/28/2007, Domenico wrote:

              >David, Erik,
              >
              >Sorry for my post being a little unclear.
              >
              >svgdoc.createElementNS(svgNS,"script")
              >
              >works for Opera and Firefox. One question was if there's an
              >alternative that works for Safari and ASV as well.

              I also was unable to get it working in ASV, whence all my dabbling
              about with DOM. In IE/ASV I was able to confirm that the <script>
              element had been created, but I could not find any way to locate the
              functions within it, nor to run said functions. With a "normal"
              inline script, the text of the script including its function are
              available as the nodeValue of the relevant tag, but such does not
              seem to be for .es/.js files.

              The one workaround that comes to mind is the following -- embed the
              SVG in HTML and then load the script tag into HTML using DOM methods.
              The following works in IE, Opera, Safari and FF (with a simple
              alert("hello") statement in the alert.js file), so that could provide
              a workaround.

              Of course if browsers other than Opera would support foreignObject,
              then we could insert a tiny HTML object in the middle of our SVG
              instead of the other way around and use it to work around what
              appears (to me) to be an IE/ASV limitation.

              David

              <html>
              <head>
              <script>
              function build(){
              S=document.createElement("script")
              S.setAttribute("src","alert.js")
              document.documentElement.appendChild(S)
              }
              </script>
              </head>
              <body onload="build()">
              </body>
              </html>
            • Domenico Strazzullo
              ... the ... I Agree. I arrived at the same conclusion. But I kind of refuse to believe this is a bug. Maybe one of the fathers of ASV would be able to shed
              Message 6 of 9 , Nov 30, 2007
                --- In svg-developers@yahoogroups.com, David Dailey
                <david.dailey@...> wrote:
                >
                > At 07:53 PM 11/28/2007, Domenico wrote:
                >
                > >David, Erik,
                > >
                > >Sorry for my post being a little unclear.
                > >
                > >svgdoc.createElementNS(svgNS,"script")
                > >
                > >works for Opera and Firefox. One question was if there's an
                > >alternative that works for Safari and ASV as well.
                >
                > I also was unable to get it working in ASV, whence all my dabbling
                > about with DOM. In IE/ASV I was able to confirm that the <script>
                > element had been created, but I could not find any way to locate
                the
                > functions within it, nor to run said functions. With a "normal"
                > inline script, the text of the script including its function are
                > available as the nodeValue of the relevant tag, but such does not
                > seem to be for .es/.js files.
                >

                I Agree. I arrived at the same conclusion. But I kind of refuse to
                believe this is a bug. Maybe one of the fathers of ASV would be able
                to shed some light over this?

                > The one workaround that comes to mind is the following -- embed the
                > SVG in HTML and then load the script tag into HTML using DOM
                methods.
                > The following works in IE, Opera, Safari and FF (with a simple
                > alert("hello") statement in the alert.js file), so that could
                provide
                > a workaround.

                I was considering this option, although the project was born as
                standalone svg. I might have to resign to it or else wait for DOM3
                load and save, but then it still wouldn't make sense if MS don't
                implement (déjà vu) and so long as we can't fix ASV :(

                >
                > Of course if browsers other than Opera would support foreignObject,
                > then we could insert a tiny HTML object in the middle of our SVG
                > instead of the other way around and use it to work around what
                > appears (to me) to be an IE/ASV limitation.
                >

                I agree again. Catch 22.

                Domenico


                > David
                >
                > <html>
                > <head>
                > <script>
                > function build(){
                > S=document.createElement("script")
                > S.setAttribute("src","alert.js")
                > document.documentElement.appendChild(S)
                > }
                > </script>
                > </head>
                > <body onload="build()">
                > </body>
                > </html>
                >
              • andre m. winter
                hi ... if i remember well (ASV is old stuff), it has to do with the fact, that script are interpreted first and only once a page is loaded. anything added
                Message 7 of 9 , Nov 30, 2007
                  hi
                  >> functions within it, nor to run said functions. With a "normal"
                  >> inline script, the text of the script including its function are
                  >> available as the nodeValue of the relevant tag, but such does not
                  >> seem to be for .es/.js files.

                  if i remember well (ASV is old stuff), it has to do with the fact, that
                  script are interpreted first and only once a page is loaded. anything
                  added later stays passive as long as you don't pass it through something
                  like eval(). of course that doesn't work for external script files. i
                  guess that was also the case for NN4 and IE5 for html pages (?).

                  andré

                  --
                  ___________________________________________________________________
                  andre m. winter,
                  cartography for internet and multimedia applications
                  schiessstand 4/1, a6091 goetzens, tyrol, austria
                  tel.: ++43.5234.32732
                  http://www.vectoreal.com/ SVG consulting and development
                  http://www.geotrace.net/ geo-localized high quality photographs
                  http://www.carto.at/ print and online touristic map solutions
                • Domenico Strazzullo
                  ... not ... that ... It figures... Here s the workaround:
                  Message 8 of 9 , Nov 30, 2007
                    --- In svg-developers@yahoogroups.com, "andre m. winter"
                    <ml.winter@...> wrote:
                    >
                    > hi
                    > >> functions within it, nor to run said functions. With a "normal"
                    > >> inline script, the text of the script including its function are
                    > >> available as the nodeValue of the relevant tag, but such does
                    not
                    > >> seem to be for .es/.js files.
                    >
                    > if i remember well (ASV is old stuff), it has to do with the fact,
                    that
                    > script are interpreted first and only once a page is loaded.

                    It figures... Here's the workaround:

                    <g id="scripts">
                    <script xlink:href="conf" type="text/ecmascript"/>
                    <script xlink:href="gui.es" type="text/ecmascript"/>
                    <script type="text/ecmascript"><![CDATA[
                    var s;
                    for (var i = 0; i < scripts.length; i ++) {
                    s = document.createElementNS
                    ("http://www.w3.org/2000/svg","script");
                    s.setAttributeNS
                    ("http://www.w3.org/1999/xlink","xlink:href",scripts[i]);
                    document.documentElement.appendChild(s);
                    }
                    ]]></script>
                    </g>

                    It works everywhere except Safari. I'll take a look but perhaps it's
                    just premature.

                    conf is controlled by php+Ajax and contains arrays with filenames,
                    preferences, skins, etc. It can be loaded and saved, and with some
                    effort reloaded. Coupled with the findings from André's
                    XMLHttpRequest thread, this should allow unrestricted local work with
                    no security breach.

                    Thanks to all.

                    Domenico


                    > anything
                    > added later stays passive as long as you don't pass it through
                    something
                    > like eval(). of course that doesn't work for external script files.
                    i
                    > guess that was also the case for NN4 and IE5 for html pages (?).
                    >
                    > andré
                    >
                    > --
                    > ___________________________________________________________________
                    > andre m. winter,
                    > cartography for internet and multimedia applications
                    > schiessstand 4/1, a6091 goetzens, tyrol, austria
                    > tel.: ++43.5234.32732
                    > http://www.vectoreal.com/ SVG consulting and development
                    > http://www.geotrace.net/ geo-localized high quality photographs
                    > http://www.carto.at/ print and online touristic map solutions
                    >
                  Your message has been successfully submitted and would be delivered to recipients shortly.