Microsoft example runs in IE7, but not in Firefox

R

RichardL

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>
 
L

Lee

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>


--
 
L

Lee

-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.


--
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
oglegroups.com>, Wed, 4 Jul 2007 21:03:19, RichardL <RichardDummyMailbox
(e-mail address removed)> 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.
 
R

RichardL

RichardL said:










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.
--
 
R

RichardL

In comp.lang.javascript message <[email protected]
oglegroups.com>, Wed, 4 Jul 2007 21:03:19, RichardL <RichardDummyMailbox
(e-mail address removed)> posted:




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. [email protected] Turnpike v6.05 IE 6
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

That's sad, if true.


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


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.


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

I meant: split long line by inserting "\<Enter key>" after say 30 or
40 characters, and do that recursively?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top