Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > annoying problem

Reply
Thread Tools

annoying problem

 
 
andreyvul
Guest
Posts: n/a
 
      02-04-2008
I'm using Firefox's Web Development toolbar and it whines about
function bar() not found:
<html>
<head>
<script type='"text/javascript">
function foo() {
document.write("<input type=\"button\" value=\"bar\" onclick=
\"bar();\">");
}

function bar() {

}
</script>
</head>
<body>
<input type="button" value="baz" onclick="foo();" />
</body>
</html>

Any clue on how to fix it without inlining the contents of bar()
inside the onclick of the button generated by foo()?
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-04-2008
andreyvul wrote:
> I'm using Firefox's Web Development toolbar and it whines about
> function bar() not found:
> <html>
> <head>
> <script type='"text/javascript">
> function foo() {
> document.write("<input type=\"button\" value=\"bar\" onclick=
> \"bar();\">");
> }
>
> [...]
> <input type="button" value="baz" onclick="foo();" />


What you presumptuously call "whining" is its correctly reporting that the
current execution context does not define a callable object that can be
referred to by this name. As you have overwritten the previous context
where that object was defined when you called document.write() after the
document was fully loaded. That behavior of the UA is in full compliance
with W3C DOM Level 2 HTML.

http://www.w3.org/TR/DOM-Level-2-HTML/

> Any clue on how to fix it without inlining the contents of bar()
> inside the onclick of the button generated by foo()?


Don't use document.write() but other DOM methods instead.

Thank you for wasting the time of other people by needlessly asking a
Frequently Asked Question.

http://jibbering.com/faq/


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$(E-Mail Removed)>
 
Reply With Quote
 
 
 
 
Joost Diepenmaat
Guest
Posts: n/a
 
      02-04-2008
andreyvul <(E-Mail Removed)> writes:

> I'm using Firefox's Web Development toolbar and it whines about
> function bar() not found:
> <html>
> <head>
> <script type='"text/javascript">
> function foo() {
> document.write("<input type=\"button\" value=\"bar\" onclick=
> \"bar();\">");
> }
>
> function bar() {
>
> }
> </script>
> </head>
> <body>
> <input type="button" value="baz" onclick="foo();" />
> </body>
> </html>
>
> Any clue on how to fix it without inlining the contents of bar()
> inside the onclick of the button generated by foo()?


document.write on a loaded page replaces the page. so no bar() for you.

use the DOM instead.

Joost.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-04-2008
Joost Diepenmaat wrote:
> document.write on a loaded page replaces the page. so no bar() for you.
>
> use the DOM instead.


JFTR: The OP is using the DOM already, only in a wrong way.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
andreyvul
Guest
Posts: n/a
 
      02-04-2008
Solution: use <div id="foo"></div> as a placeholder and use
document.getElementById("foo").innerHTML= instead of
document.write( ).
Problem solved.
Is this the Right Way of using the DOM method for dynamic content
generation?
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-04-2008
andreyvul wrote:
> Solution: use <div id="foo"></div> as a placeholder and use
> document.getElementById("foo").innerHTML= instead of
> document.write( ).
> Problem solved.


In your test case.

> Is this the Right Way of using the DOM method for dynamic content
> generation?


No. RTFM, STFG, STFW.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
Jeremy J Starcher
Guest
Posts: n/a
 
      02-05-2008
On Mon, 04 Feb 2008 15:05:50 -0800, andreyvul wrote:

> Solution: use <div id="foo"></div> as a placeholder and use
> document.getElementById("foo").innerHTML= instead of document.write( ).
> Problem solved.
> Is this the Right Way of using the DOM method for dynamic content
> generation?


..innerHTML is not a DOM standard (yet). Although a lot of browsers
support it, not all do. Even among those that do support it, it has many
well-known issues. I recommend avoiding it unless you understand those
issues.

< URL http://inmyexperience.com/archives/000428.shtml >

createElement and and that family of calls are a far better way to
make the changes, though you should make sure that they are available
before calling them blindly.

Check google groups for 'Code Worth Recommending Project" and "gebi" for
a rather long but good discussion about how to "feature test" before
blindly calling these functions.

 
Reply With Quote
 
andreyvul
Guest
Posts: n/a
 
      02-05-2008
On Feb 4, 7:45 pm, Jeremy J Starcher <(E-Mail Removed)> wrote:
> On Mon, 04 Feb 2008 15:05:50 -0800, andreyvul wrote:
> > Solution: use <div id="foo"></div> as a placeholder and use
> > document.getElementById("foo").innerHTML= instead of document.write( ).
> > Problem solved.
> > Is this the Right Way of using the DOM method for dynamic content
> > generation?

>
> .innerHTML is not a DOM standard (yet). Although a lot of browsers
> support it, not all do. Even among those that do support it, it has many
> well-known issues. I recommend avoiding it unless you understand those
> issues.
>
> < URLhttp://inmyexperience.com/archives/000428.shtml>
>
> createElement and and that family of calls are a far better way to
> make the changes, though you should make sure that they are available
> before calling them blindly.
>
> Check google groups for 'Code Worth Recommending Project" and "gebi" for
> a rather long but good discussion about how to "feature test" before
> blindly calling these functions.


I'm looking at the DOM specs yet I can't find way to add raw HTML
inside a <div></div> without a chain of createElement() calls.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-05-2008
andreyvul wrote:
> On Feb 4, 7:45 pm, Jeremy J Starcher <(E-Mail Removed)> wrote:
>> Check google groups for 'Code Worth Recommending Project" and "gebi" for
>> a rather long but good discussion about how to "feature test" before
>> blindly calling these functions.

>
> I'm looking at the DOM specs yet I can't find way to add raw HTML
> inside a <div></div> without a chain of createElement() calls.


The reason you can't find anything else in the specs is that there is no
other standards-compliant way (yet?). However, try to think of that as an
advantage. You may need more calls but in a conforming implementation you
won't be able to generate any invalid markup, much in contrast to using
innerHTML, and if something fails you stay in control in every step on the
way. It would appear that this is the main reason why `innerHTML' was not
standardized (yet?).


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
Jeremy J Starcher
Guest
Posts: n/a
 
      02-05-2008
On Mon, 04 Feb 2008 18:53:20 -0800, andreyvul wrote:

> I'm looking at the DOM specs yet I can't find way to add raw HTML
> inside a <div></div> without a chain of createElement() calls.


*nods*

Depending on exactly what you are doing, there are a few shortcuts that
can be taken. You could create your HTML as regular HTML in the page,
hide it with CSS, and move the node to where you need it later.

This works on modern browsers. Fails with my cell phone though, I end up
seeing stray HTML at the bottom of the page.

Google for 'innerHTML replacement' for some other ideas.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Annoying Problem While Debugging ASP.NET Joe ASP .Net 2 01-04-2006 11:14 AM
Re: Annoying HttpModule/QueryString Problem Daniel Fisher\(lennybacon\) ASP .Net 0 11-30-2005 08:51 AM
Annoying HttpModule/QueryString Problem Andreas Zita ASP .Net 0 11-29-2005 07:30 PM
annoying SP2 wireless problem =?Utf-8?B?R3JlZw==?= Wireless Networking 1 08-16-2004 10:16 PM
annoying IE problem with dashed CSS borders TheKeith HTML 15 06-30-2003 07:17 AM



Advertisments