alert inside <DIV>

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

    #1

    alert inside <DIV>

    Hi,

    I defined:
    <div id="abc">
    </div>

    in my .html file, after an Ajax query my weberserver sends:
    <script language="JavaS cript">
    alert('Hello World!");
    </script>

    which should be included in the div-block.

    But the message box doesnt appear.
    If i send just pure text the text is shown in the browser.

    How is it possible to call a Meessage box inside a div block?

  • Randy Webb

    #2
    Re: alert inside &lt;DIV&gt;

    joe said the following on 11/16/2006 9:38 AM:
    Hi,
    >
    I defined:
    <div id="abc">
    </div>
    >
    in my .html file, after an Ajax query my weberserver sends:
    Fix your server so it sends a proper script element. language is
    deprecated, use the type attribute instead.
    <script language="JavaS cript">
    alert('Hello World!");
    </script>
    >
    which should be included in the div-block.
    And it gets included. Alert the innerHTML and it is there.
    But the message box doesnt appear.
    If i send just pure text the text is shown in the browser.
    >
    How is it possible to call a Meessage box inside a div block?
    Script elements that are inserted via innerHTML do not, with two
    exceptions[1], get executed. You will have to parse out the script block
    itself, and then use createElement to create a script block. I can't
    find the particular thread where this was discussed, in depth, but if
    you search the archives for "Randy Webb Dynamic loadJSFile" you can get
    some hits where you can read a lot about it. Basically, you would find
    the script elements in your HTMLFragment, then call a function like this:

    function executeJSCode(s tringToExecute) {
    var newScript = document.create Element('script ');
    newScript.type = "text/javascript";
    newScript.text = stringToExecute ;
    document.getEle mentsByTagName( 'head')[0].appendChild(ne wScript);
    }

    If you absolutely need it in the div element, change it to
    document.getEle mentById('someD iv').appendChil d(newScript)

    And call it as such:

    executeJSCode(' String of code here without script tags')

    or:

    executeJSCode(s tringOfCode)

    Where stringOfCode is a variable that holds your script block's text. Be
    aware though that if your code has a document.write statement in it then
    it is not going to work properly (It will wipe out the page).

    [1]
    Exception 1: IE will execute script inserted via innerHTML if the script
    element has the DEFER attribute.
    Exception 2: NS6 (very early versions) would execute script inserted via
    innerHTML

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javas cript FAQ - http://jibbering.com/faq
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

    Comment

    • joe

      #3
      Re: alert inside &lt;DIV&gt;


      Randy Webb schrieb:

      Fix your server so it sends a proper script element. language is
      deprecated, use the type attribute instead.
      ok. now the server sends:
      <script type="text/javascript">
      alert(Executed" );
      </script>"
      which should be included in the div-block.
      >
      And it gets included. Alert the innerHTML and it is there.
      how?

      Script elements that are inserted via innerHTML do not, with two
      exceptions[1], get executed. You will have to parse out the script block
      itself, and then use createElement to create a script block. Basically, you would find
      the script elements in your HTMLFragment, then call a function like this:
      function executeJSCode(s tringToExecute) {
      var newScript = document.create Element('script ');
      newScript.type = "text/javascript";
      newScript.text = stringToExecute ;
      document.getEle mentsByTagName( 'head')[0].appendChild(ne wScript);
      }
      >
      If you absolutely need it in the div element, change it to
      document.getEle mentById('someD iv').appendChil d(newScript)
      >
      And call it as such:
      >
      executeJSCode(' String of code here without script tags')
      its not invoked, and i would be very surprised if it would, cause its a
      javascript function like alert.

      Comment

      • Laurent Bugnion

        #4
        Re: alert inside &lt;DIV&gt;

        Hi,

        joe wrote:
        Randy Webb schrieb:
        >
        >
        >Fix your server so it sends a proper script element. language is
        >deprecated, use the type attribute instead.
        >
        ok. now the server sends:
        <script type="text/javascript">
        alert(Executed" );
        </script>"
        That's better.
        >which should be included in the div-block.
        >>
        >And it gets included. Alert the innerHTML and it is there.
        >
        how?
        Randy means that if you include the script block in the DIV, and then
        you get the DIV by ID and alert the innerHTML property, you will see
        that the script was indeed included.

        doSomethingToIn cludeScript();
        var nDiv = document.getEle mentById( "divScriptTarge t" );
        alert( nDiv.innerHTML );
        >Script elements that are inserted via innerHTML do not, with two
        >exceptions[1], get executed. You will have to parse out the script block
        >itself, and then use createElement to create a script block. Basically, you would find
        >the script elements in your HTMLFragment, then call a function like this:
        >
        >function executeJSCode(s tringToExecute) {
        >var newScript = document.create Element('script ');
        >newScript.ty pe = "text/javascript";
        >newScript.te xt = stringToExecute ;
        >document.getEl ementsByTagName ('head')[0].appendChild(ne wScript);
        >}
        >>
        >If you absolutely need it in the div element, change it to
        >document.getEl ementById('some Div').appendChi ld(newScript)
        >>
        >And call it as such:
        >>
        >executeJSCode( 'String of code here without script tags')
        >
        its not invoked, and i would be very surprised if it would, cause its a
        javascript function like alert.
        What Randy means is that after including your script code (which should
        be contained in a method), you must invoke the method yourself.

        Something like:

        SERVER SENDS:

        <script type="text/javascript">
        function alertExecuted()
        {
        alert( "Executed" );
        }
        </script>

        CLIENT SCRIPT:

        doSomethingToIn cludeScript();
        alertExecuted() ;

        The script won't be executed just because it's included (except in the 2
        exceptions listed by Randy). You must trigger it yourself.

        HTH,
        Laurent
        --
        Laurent Bugnion, GalaSoft
        Software engineering: http://www.galasoft-LB.ch
        PhotoAlbum: http://www.galasoft-LB.ch/pictures
        Support children in Calcutta: http://www.calcutta-espoir.ch

        Comment

        • Randy Webb

          #5
          Re: alert inside &lt;DIV&gt;

          joe said the following on 11/20/2006 12:08 PM:
          Randy Webb schrieb:
          >
          >
          >Fix your server so it sends a proper script element. language is
          >deprecated, use the type attribute instead.
          >
          ok. now the server sends:
          <script type="text/javascript">
          alert(Executed" );
          </script>"
          I hope that was a typo as the alert doesn't have an opening " and you
          have an errant " after the script tag:

          <script type="text/javascript">
          alert("Executed ");
          </script>
          >which should be included in the div-block.
          >>
          >And it gets included. Alert the innerHTML and it is there.
          >
          how?
          In FF2.0 and Opera 9, you can alert
          document.getEle mentById('someD iv').innerHTML and see the code you just
          put into it. With IE7, it seems that if the only thing going in is a
          script block then it blows up and forgets about it. See the test page at
          the bottom, along with a solution.
          >Script elements that are inserted via innerHTML do not, with two
          >exceptions[1], get executed. You will have to parse out the script block
          >itself, and then use createElement to create a script block. Basically, you would find
          >the script elements in your HTMLFragment, then call a function like this:
          >
          >function executeJSCode(s tringToExecute) {
          >var newScript = document.create Element('script ');
          >newScript.ty pe = "text/javascript";
          >newScript.te xt = stringToExecute ;
          >document.getEl ementsByTagName ('head')[0].appendChild(ne wScript);
          >}
          >>
          >If you absolutely need it in the div element, change it to
          >document.getEl ementById('some Div').appendChi ld(newScript)
          >>
          >And call it as such:
          >>
          >executeJSCode( 'String of code here without script tags')
          >
          its not invoked, and i would be very surprised if it would, cause its a
          javascript function like alert.
          Did you test it with a valid script statement? The above code works.
          Even in IE. The problem comes elsewhere trying to read those script
          elements. I didn't post code to do it (I did this time) to "parse out
          the script block itself".

          Create a test page:

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
          "http://www.w3.org/TR/REC-html40/strict.dtd">
          <html>
          <head>
          <title>Executin g Script blocks inserted via innerHTML</title>
          <script type="text/javascript">
          function loadHTMLFragmen t(someFragment) {
          document.getEle mentById('myDiv ').innerHTML = '<br>' + someFragment;
          //notes below on that br element
          var d = document.getEle mentById('myDiv ').getElementsB yTagName("scrip t");
          var t = d.length;
          for (var x=0;x<t;x++){;
          var newScript = document.create Element('script ');
          newScript.type = "text/javascript";
          newScript.text = d[x].text;
          document.getEle mentById('myDiv ').appendChild( newScript);
          }
          }
          </script>
          <body>
          <div id="myDiv"></div>
          <textarea onchange="loadH TMLFragment(thi s.value)" rows="15"
          cols="80"></textarea>
          </body>
          </html>

          Open that page and paste this snippet into the textarea:

          <script type="text/javascript">
          alert("Executed ");
          </script>

          And then blur the text area. In Opera9 and FF2.0 you get that alert.

          In IE7, it seems that if the very first element is a <scriptelemen t,
          then it won't execute it and gives an error that .text is undefined.
          But, adding a second <scriptblock it works and executes both. Adding
          any other HTML element before the <scriptblock makes it work
          flawlessly whether there is one or more script elements. So, I added
          that leading <brtag in the code above and it seems to have satisfied IE.

          --
          Randy
          Chance Favors The Prepared Mind
          comp.lang.javas cript FAQ - http://jibbering.com/faq
          Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

          Comment

          Working...