Jim Davis said the following on 7/20/2006 11:56 PM:
Quote:
"Randy Webb" <HikksNotAtHome@aol.comwrote in message
news:n7qdnfkA1IcJt13ZnZ2dnUVZ_vGdnZ2d@comcast.com. ..
Quote:
>Jim Davis said the following on 7/20/2006 11:42 AM:
<snip>
Quote:
Quote:
Quote:
>>So to just let injected script run (in IE) you need to just set the
>>"defer" attribute.
>Or extract the text of the script element, re-create it and you are done
>in any browser that supports createElement and appendChild
>
Of course your solution has one caveat...
If I were writing code for production use, it wouldn't have that caveat
but you are right.
Quote:
IE will run the code as is using "defer", Firefox will not. Using just the
script recreation code you posted with "defer" will actually run the code
TWICE in IE.
Then leave the defer out, create your own element, then you have nothing
to worry about.
Quote:
You can simply run the script without "defer" of course - and this is by far
the simplest method, but you are doing work not required by IE.
But by including the defer you are creating double work for yourself.
Quote:
If you've got a lot of code then you may want to make the decision to
do a bit more complex and only run the script recreation in Firefox and
not in IE.
Run the script recreation in both, simple solution to a simple problem.
Don't fall into the trap of trying to make it more difficult than it is.
Quote:
Also note that, as is, your code will actually create duplicate script
blocks (dump the contents of the DIV to see this). I would take the effort
to remove the original code blocks. Adding a line like the following within
your loop should do it I think:
>
d[x].parentNode.removeChild(d[x]);
Yes, and I didn't write production code. I wouldn't use either method to
be honest with you in real time production code.
Quote:
In initial tests this seems to work fine. I would have liked to simply
replace the original blocks with the new blocks but I couldn't discover a
method that would both replace the block AND run it.
You won't. And that leads to a potential flaw in the entire process.
Retrieve this simple document using this new fangled technology called
"AJAX", read the script block, execute it, then view source.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<html>
<head>
<title>Input Questions</title>
</head>
<body>
<script type="text/javascript">
document.write('My name is Fred Flintstone');
</script>
</body>
</html>
Back to the drawing board Barney :)
And the problem will occur with any script block that uses
document.write to create content when it is retrieved, parsed, and executed.
<snip>
Quote:
As I noted when falling on my sword with Richard after testing it I
discovered that I was indeed wrong. I'm going to do a little more digging
since I wrote the original code under IE 5 and I can't test that readily.
I'm generally pretty anal about commenting all "odd" things and this has a
large, elborate comment concerning the need to "null" the container.
It may be that IE5.0 has some idiotic flaw in it that requires the null
or '' to the container. If it does, that is just one more reason *not*
to depend on defer and innerHTML to execute them.
The backwards compatibility not withstanding, then is one of the reasons
that I have said for almost a year now that I don't use AJAX in a real
world site because I know of a better, simpler, method to do what people
are calling "AJAX" that is more reliable and more widely supported.
Quote:
Even if I'm wrong I'm curious how I came to the conclusion. It may be for
some other reason and I'm misrembering. In any case I'm definately wrong
concerning IE 6.x.
See above.
Quote:
Oh... one more aspect of this when injecting script into DIVs to simulate
new "pages".
It's not an "aspect", its a misunderstanding.
Quote:
Injecting the script element is all well and good, but as we've seen doing
so is the same as running that script globally: replacing the content of the
DIV does not (as you and Richard have pointed out) eliminate script
declaration previously made regardless of the content of the DIV. Functions
set, global variables declared, etc will persist unless explicitly
overridden.
No, it will persist until nothing points to it anymore. Garbage
Collection is your friend
Quote:
Automatically "knowing" or discovering which declarations were made by the
script blocks in question could be nightmarishly difficult.
I don't see a reason to need to know except when debugging. And yes, it
would become a nightmare.
Quote:
To address this, as I mentioned before, you can create a container for the
page-specific material - a sort of pseudo-scope. Create, for example, a
global object called "Page" and place all of your page specific functions,
variables, etc within it.
>
For example:
>
Page = new Object();
>
Page.myVar = value;
>
Page.Init = function() {};
>
.... and so forth.
>
If each "page" loaded reinitializes that "scope" object (or, better, a
global page loader method manages it) then you'll be reducing the amount of
"baggage" left around as pages are loaded and the application is used.
Probably. But I don't use the HTTPRequest Object to retrieve files for
the "speed" aspect of "Web 2.0" that uses what is called "AJAX". I use a
more reliable, more cross-browser, method that has it's own quirks but
it doesn't have near as many as HTTPRequest does.
--
Randy
comp.lang.javascript FAQ -
http://jibbering.com/faq & newsgroup weekly
Temporarily at:
http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/