Microsoft example runs in IE7, but not in Firefox

Discussion in 'Javascript' started by RichardL, Jul 4, 2007.

  1. RichardL

    RichardL Guest

    The following script from http://msdn2.microsoft.com/en-us/library/ms533897.aspx
    runs fine in HTML-Kit 1.0.0.292 and IE 7.0, but not in Firefox
    2.0.0.4.

    In Firefox, it opens a blank page, but does not present the "Click
    Me" button. I suspected the "DEFER" attribute might be a Microsoft-
    only invention, but removing it did not improve the situation.

    Any ideas?

    <head>
    <title>EmbeddingScriptExample</title>
    <script type="text/javascript">
    function insertScript(){
    var sHTML="<input type=button onclick=" + "go2()" + "
    value='Click Me'><BR>";
    var sScript="<SCRIPT DEFER>";
    sScript = sScript + "function go2(){ alert('Hello from
    inserted script.') }";
    sScript = sScript + "</SCRIPT" + ">";
    ScriptDiv.innerHTML = sHTML + sScript;
    }
    </script>
    </head>

    <body onload="insertScript();">
    <DIV ID="ScriptDiv"></DIV>
    </body>
    RichardL, Jul 4, 2007
    #1
    1. Advertising

  2. RichardL

    Lee Guest

    RichardL said:
    >
    >The following script from http://msdn2.microsoft.com/en-us/library/ms533897.aspx
    >runs fine in HTML-Kit 1.0.0.292 and IE 7.0, but not in Firefox
    >2.0.0.4.
    >
    >In Firefox, it opens a blank page, but does not present the "Click
    >Me" button. I suspected the "DEFER" attribute might be a Microsoft-
    >only invention, but removing it did not improve the situation.
    >
    >Any ideas?
    >
    ><head>
    > <title>EmbeddingScriptExample</title>
    > <script type="text/javascript">
    > function insertScript(){
    > var sHTML="<input type=button onclick=" + "go2()" + "
    >value='Click Me'><BR>";
    > var sScript="<SCRIPT DEFER>";
    > sScript = sScript + "function go2(){ alert('Hello from
    >inserted script.') }";
    > sScript = sScript + "</SCRIPT" + ">";
    > ScriptDiv.innerHTML = sHTML + sScript;
    > }
    > </script>
    ></head>
    >
    ><body onload="insertScript();">
    > <DIV ID="ScriptDiv"></DIV>
    ></body>
    >


    If you're not seeing the button, you've typed it in wrong.
    When I copy and paste your code, being careful to repair the
    wrapped lines, I see the button in the same version of Firefox.
    When clicked, nothing happens, but when I look at the Error
    Console, I see the expected message: "go2 is not defined".

    If you want it to actually work, you should use DOM methods
    instead of using innerHTML to add script. You should really
    use DOM in place of innerHTML, period, but I've just replaced
    the script portion:

    <html>
    <head>
    <title>EmbeddingScriptExample</title>
    <script type="text/javascript">
    function insertScript(){
    var ScriptDiv=document.getElementById("ScriptDiv");
    var sHTML="<input type='button' onclick='go2()' value='Click Me'><BR>";
    ScriptDiv.innerHTML = sHTML;

    var scriptEl=document.createElement("script");
    scriptEl.setAttribute("type","text/javascript");
    scriptEl.text="function go2(){ alert('Hello from inserted script.') }";
    ScriptDiv.appendChild(scriptEl);

    }
    </script>
    </head>

    <body onload="insertScript();">
    <DIV ID="ScriptDiv"></DIV>
    </body>
    </html>


    --
    Lee, Jul 5, 2007
    #2
    1. Advertising

  3. RichardL

    Lee Guest

    -Lost said:

    >
    >Ooo, IE7 doesn't puke on setAttribute?


    Apparently not. It was the last line I added, and certainly
    isn't necessary, but my IE didn't complain about it.


    --
    Lee, Jul 5, 2007
    #3
  4. In comp.lang.javascript message <
    oglegroups.com>, Wed, 4 Jul 2007 21:03:19, RichardL <RichardDummyMailbox
    > posted:
    >The following script from http://msdn2.microsoft.com/en-us/library/ms533897.aspx
    >runs fine in HTML-Kit 1.0.0.292 and IE 7.0, but not in Firefox
    >2.0.0.4.
    >
    >In Firefox, it opens a blank page, but does not present the "Click
    >Me" button. I suspected the "DEFER" attribute might be a Microsoft-
    >only invention, but removing it did not improve the situation.
    >
    >Any ideas?


    Microsoft code is probably designed, where possible, not to run on other
    systems.

    Use, in Firefox, Tools Error Console and it will tell you exactly
    what is first found wrong.

    Also, "</SCRIPT" should really be "<\/SCRIPT" .

    Don't allow your posting agent to wrap code; read the FAQ.

    It's a good idea to read the newsgroup c.l.j and its stale FAQ. See below.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr J R Stockton, Jul 5, 2007
    #4
  5. RichardL

    RichardL Guest

    On Jul 4, 7:18 pm, Lee <> wrote:
    > RichardL said:
    >
    >
    >
    >
    >
    > >The following script fromhttp://msdn2.microsoft.com/en-us/library/ms533897.aspx
    > >runs fine in HTML-Kit 1.0.0.292 and IE 7.0, but not in Firefox
    > >2.0.0.4.

    >
    > >In Firefox, it opens a blank page, but does not present the "Click
    > >Me" button. I suspected the "DEFER" attribute might be a Microsoft-
    > >only invention, but removing it did not improve the situation.

    >
    > >Any ideas?

    >
    > ><head>
    > > <title>EmbeddingScriptExample</title>
    > > <script type="text/javascript">
    > > function insertScript(){
    > > var sHTML="<input type=button onclick=" + "go2()" + "
    > >value='Click Me'><BR>";
    > > var sScript="<SCRIPT DEFER>";
    > > sScript = sScript + "function go2(){ alert('Hello from
    > >inserted script.') }";
    > > sScript = sScript + "</SCRIPT" + ">";
    > > ScriptDiv.innerHTML = sHTML + sScript;
    > > }
    > > </script>
    > ></head>

    >
    > ><body onload="insertScript();">
    > > <DIV ID="ScriptDiv"></DIV>
    > ></body>

    >
    > If you're not seeing the button, you've typed it in wrong.
    > When I copy and paste your code, being careful to repair the
    > wrapped lines, I see the button in the same version of Firefox.
    > When clicked, nothing happens, but when I look at the Error
    > Console, I see the expected message: "go2 is not defined".
    >
    > If you want it to actually work, you should use DOM methods
    > instead of using innerHTML to add script. You should really
    > use DOM in place of innerHTML, period, but I've just replaced
    > the script portion:
    >
    > <html>
    > <head>
    > <title>EmbeddingScriptExample</title>
    > <script type="text/javascript">
    > function insertScript(){
    > var ScriptDiv=document.getElementById("ScriptDiv");
    > var sHTML="<input type='button' onclick='go2()' value='Click Me'><BR>";
    > ScriptDiv.innerHTML = sHTML;
    >
    > var scriptEl=document.createElement("script");
    > scriptEl.setAttribute("type","text/javascript");
    > scriptEl.text="function go2(){ alert('Hello from inserted script.') }";
    > ScriptDiv.appendChild(scriptEl);
    >
    > }
    > </script>
    > </head>
    >
    > <body onload="insertScript();">
    > <DIV ID="ScriptDiv"></DIV>
    > </body>
    > </html>
    >
    > --


    I didn't know about Error Console. Thanks for alerting me to that.

    Strange: my error message is "ScriptDiv is not defined."

    Your code works great in IE, HTML-Kit and Firefox. I will strive to
    adopt that style. In the meantime, I want to follow up with this
    innerHTML thing because I think it's used in the routines supporting
    Ajax in the Ruby on Rails environment. Now that I know about Error
    Console, I'm optimistic I'll get somewhere.
    --
    RichardL, Jul 5, 2007
    #5
  6. RichardL

    RichardL Guest

    On Jul 5, 7:11 am, Dr J R Stockton <> wrote:
    > In comp.lang.javascript message <
    > oglegroups.com>, Wed, 4 Jul 2007 21:03:19, RichardL <RichardDummyMailbox
    > > posted:
    >
    > >The following script fromhttp://msdn2.microsoft.com/en-us/library/ms533897.aspx
    > >runs fine in HTML-Kit 1.0.0.292 and IE 7.0, but not in Firefox
    > >2.0.0.4.

    >
    > >In Firefox, it opens a blank page, but does not present the "Click
    > >Me" button. I suspected the "DEFER" attribute might be a Microsoft-
    > >only invention, but removing it did not improve the situation.

    >
    > >Any ideas?

    >
    > Microsoft code is probably designed, where possible, not to run on other
    > systems.
    >
    > Use, in Firefox, Tools Error Console and it will tell you exactly
    > what is first found wrong.
    >
    > Also, "</SCRIPT" should really be "<\/SCRIPT" .
    >
    > Don't allow your posting agent to wrap code; read the FAQ.
    >
    > It's a good idea to read the newsgroup c.l.j and its stale FAQ. See below.
    >
    > --
    > (c) John Stockton, Surrey, UK. ? Turnpike v6.05 IE 6
    > news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    > <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    > <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


    > Microsoft code is probably designed, where possible,
    > not to run on other systems.


    That's sad, if true.

    > Use, in Firefox, Tools Error Console

    Thanks for that. Lee had mentioned also. I hadn't known about it.

    > Also, "</SCRIPT" should really be "<\/SCRIPT" .


    Where is that documented. I've been using http://www.w3schools.com/js/js_examples.asp
    as a tutorial. They don't escape the forward slash in any of them,
    and they seem to run OK in IE7, Firefox 2 and HTML-Kit bld 292.


    > Don't allow your posting agent to wrap code; read the FAQ.


    I read the FAQ. Do you/they mean end long lines with a \<Enter key> ?
    --
    R
    RichardL, Jul 7, 2007
    #6
  7. RichardL

    RichardL Guest

    On Jul 7, 6:02 pm, RichardL
    <> wrote:
    > On Jul 5, 7:11 am, Dr J R Stockton <> wrote:
    >
    >
    >
    > > In comp.lang.javascript message <
    > > oglegroups.com>, Wed, 4 Jul 2007 21:03:19, RichardL <RichardDummyMailbox
    > > > posted:

    >
    > > >The following script fromhttp://msdn2.microsoft.com/en-us/library/ms533897.aspx
    > > >runs fine in HTML-Kit 1.0.0.292 and IE 7.0, but not in Firefox
    > > >2.0.0.4.

    >
    > > >In Firefox, it opens a blank page, but does not present the "Click
    > > >Me" button. I suspected the "DEFER" attribute might be a Microsoft-
    > > >only invention, but removing it did not improve the situation.

    >
    > > >Any ideas?

    >
    > > Microsoft code is probably designed, where possible, not to run on other
    > > systems.

    >
    > > Use, in Firefox, Tools Error Console and it will tell you exactly
    > > what is first found wrong.

    >
    > > Also, "</SCRIPT" should really be "<\/SCRIPT" .

    >
    > > Don't allow your posting agent to wrap code; read the FAQ.

    >
    > > It's a good idea to read the newsgroup c.l.j and its stale FAQ. See below.

    >
    > > --
    > > (c) John Stockton, Surrey, UK. ? Turnpike v6.05 IE 6
    > > news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    > > <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    > > <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    > > Microsoft code is probably designed, where possible,
    > > not to run on other systems.

    >
    > That's sad, if true.
    >
    > > Use, in Firefox, Tools Error Console

    >
    > Thanks for that. Lee had mentioned also. I hadn't known about it.
    >
    > > Also, "</SCRIPT" should really be "<\/SCRIPT" .

    >
    > Where is that documented. I've been usinghttp://www.w3schools.com/js/js_examples.asp
    > as a tutorial. They don't escape the forward slash in any of them,
    > and they seem to run OK in IE7, Firefox 2 and HTML-Kit bld 292.
    >
    > > Don't allow your posting agent to wrap code; read the FAQ.

    >
    > I read the FAQ. Do you/they mean end long lines with a \<Enter key> ?
    > --
    > R


    I meant: split long line by inserting "\<Enter key>" after say 30 or
    40 characters, and do that recursively?
    RichardL, Jul 7, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollie
    Replies:
    0
    Views:
    367
    Ollie
    Oct 2, 2003
  2. Replies:
    9
    Views:
    578
  3. Aidan
    Replies:
    4
    Views:
    504
    Neredbojias
    Jun 7, 2007
  4. OccasionalFlyer

    Page shows in FIrefox, IE7 but not IE6

    OccasionalFlyer, Dec 4, 2008, in forum: HTML
    Replies:
    4
    Views:
    439
    Chaddy2222
    Dec 5, 2008
  5. APA
    Replies:
    0
    Views:
    1,262
Loading...

Share This Page