473,382 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

innerHTML and SCRIPT tag

PJ
Greetings...

I have stumbled upon a small problem.

I use Ajax to retrieve part of a page I need to update. I update a DIV
element with the HTML contents I get from another page.

It works fine.

However the HTML have a SCRIPT tag that the browser should process, but
it doesn't.

Here's an example:

--- pageX.aspx ---
<table>
<tr>
<td>Table 01</td>
</tr>
</table>
<script>
alert("HI");
</script>
--- end pageX.aspx ---

--- page on browser ---
<div id="divContents"></div>

<script>
divContents.innerHTML = getHtmlFromPage("pageX.aspx");
</script>
--- end page on browser ---

When the prowser gets the "pageX.aspx" and updates the contents of the
'divContents' it displays the table, but it didn't process the script.

What am I doing wrong?

Regards,

PJ
http://pjondevelopment.50webs.com

Jul 19 '06 #1
17 34637
PJ said the following on 7/19/2006 10:43 AM:
Greetings...

I have stumbled upon a small problem.

I use Ajax to retrieve part of a page I need to update. I update a DIV
element with the HTML contents I get from another page.

It works fine.
It works in IE, it won't work in any other browser since you are relying
on an IE-shortcut to get a reference to the div tag.
However the HTML have a SCRIPT tag that the browser should process, but
it doesn't.
Script blocks inserted via innerHTML don't get executed in any browser
other than NS6

<snip>
When the prowser gets the "pageX.aspx" and updates the contents of the
'divContents' it displays the table, but it didn't process the script.
What am I doing wrong?
You will have to search through your HTML block and find the script
elements and insert them using createElement to get the script blocks
executed.

var d =
document.getElementById('divContents').getElements ByTagName("script")
var t = d.length
for (var x=0;x<t;x++){
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = d[x].text;
document.getElementById('divContents').appendChild (newScript);

}
--
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/
Jul 19 '06 #2

Randy Webb wrote:
You will have to search through your HTML block and find the script
elements and insert them using createElement to get the script blocks
executed.

var d =
document.getElementById('divContents').getElements ByTagName("script")
var t = d.length
for (var x=0;x<t;x++){
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = d[x].text;
document.getElementById('divContents').appendChild (newScript);
I haven't seen this method before. I've been taking another approach
that uses eval and wonder if one way is superiour (faster, more robust
etc).

function update(element, html) {
document.getElementById(element).innerHTML=html;

var re = /<script\b[\s\S]*?>([\s\S]*?)<\//ig;
var match;
while (match = re.exec(html)) {
eval(match[1]);
}
};

Thanks,
Peter

Jul 20 '06 #3
pe**********@gmail.com wrote:
Randy Webb wrote:
>You will have to search through your HTML block and find
the script elements and insert them using createElement to
get the script blocks executed.

var d =
document.getElementById('divContents').getElement sByTagName("script")
var t = d.length
for (var x=0;x<t;x++){
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = d[x].text;
document.getElementById('divContents').appendChil d(newScript);

I haven't seen this method before. I've been taking another approach
that uses eval and wonder if one way is superiour (faster, more robust
etc).

function update(element, html) {
document.getElementById(element).innerHTML=html;

var re = /<script\b[\s\S]*?>([\s\S]*?)<\//ig;
var match;
while (match = re.exec(html)) {
eval(match[1]);
}
};
They are not equivalent so comparison is irrelevant. If you - eval -
code - var - will create function local variables instead of global ones
and function declarations will be inner functions not global ones.

Richard.
Jul 20 '06 #4
pe**********@gmail.com said the following on 7/19/2006 8:27 PM:
Randy Webb wrote:
>You will have to search through your HTML block and find the script
elements and insert them using createElement to get the script blocks
executed.

var d =
document.getElementById('divContents').getElement sByTagName("script")
var t = d.length
for (var x=0;x<t;x++){
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = d[x].text;
document.getElementById('divContents').appendChil d(newScript);

I haven't seen this method before. I've been taking another approach
that uses eval and wonder if one way is superiour (faster, more robust
etc).
Yes, one way is superior to the other. You can read Richard's reply for
an explanation of the scope issues involved with the eval portion.

Second. Where is your script block appended? And, how would you go about
removing it? With the above code, you can simply set the innerHTML of
divContents to '' and you have removed *all* script blocks that were
appended. Meaning, when a new request is made, you are not retaining all
of your old script blocks but they are discarded.

And that is not even getting into the aspects of eval.

--
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/
Jul 20 '06 #5
Second. Where is your script block appended?

If I do

var d = document.getElementById('my_div');
d.innerHTML = htmlInAjaxResponse;

and htmlInAjaxResponse has script blocks in it, then are the script
blocks not inside "my_div"? If not where are they? If so then will they
not be removed when I use d=''; ?

Thanks,
Peter

Jul 20 '06 #6
pe**********@gmail.com said the following on 7/19/2006 10:14 PM:
>Second. Where is your script block appended?

If I do

var d = document.getElementById('my_div');
d.innerHTML = htmlInAjaxResponse;

and htmlInAjaxResponse has script blocks in it, then are the script
blocks not inside "my_div"?
The code for them is, the code that got executed, and its scope, is not.
If not where are they? If so then will they not be removed when I use d=''; ?
There is a difference between the code you read in and execute (it's
source) and the code that is actually executed, along with it's scope chain.

Besides, even if they are exactly the same results (they aren't always),
the first *has* to be more efficient since it isn't starting up the
parser every time you eval something.

--
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/
Jul 20 '06 #7
"Randy Webb" <Hi************@aol.comwrote in message
news:3P******************************@comcast.com. ..
PJ said the following on 7/19/2006 10:43 AM:
>Greetings...
*snip*
>However the HTML have a SCRIPT tag that the browser should process, but
it doesn't.

Script blocks inserted via innerHTML don't get executed in any browser
other than NS6
One wrinkle to this.

IE will parse/execute a script block injected into a DIV - however you must
first set the innerHTML property to "null" THEN load the content as in:

document.getElementById(ElementID).innerHTML = null;
document.getElementById(ElementID).innerHTML = NewContent;

I've not (yet?) been able to get FireFox to do the same thing (at least not
without the kind of DOM parsing you mention).

I use the above extensively in a windowing system for intranet applications
(although this works in a normal HTML file we use it in corporate HTA
applications).

Jim Davis
Jul 20 '06 #8

"Jim Davis" <ne********@vboston.comwrote in message
news:dr******************************@giganews.com ...
"Randy Webb" <Hi************@aol.comwrote in message
news:3P******************************@comcast.com. ..
>PJ said the following on 7/19/2006 10:43 AM:
>>Greetings...

*snip*
>>However the HTML have a SCRIPT tag that the browser should process, but
it doesn't.

Script blocks inserted via innerHTML don't get executed in any browser
other than NS6

One wrinkle to this.

IE will parse/execute a script block injected into a DIV - however you
must first set the innerHTML property to "null" THEN load the content as
in:

document.getElementById(ElementID).innerHTML = null;
document.getElementById(ElementID).innerHTML = NewContent;
Actually I've fibbed a little. More information:

To get the injected script to actually run in IE you also need to set the
"defer" attribute on the script element to "defer" as in:

<script type="text/javascript" defer="defer">
alert("My Script's a runnin'!");
</script>

So to just let injected script run (in IE) you need to just set the "defer"
attribute.

Setting the innerHTML to null is not actually required to run the script
however it's still a VERY good idea. The reason is because IE will not
automatically over-write any existing functions unless you do this. Setting
the innerHTML to null actually unloads all functions defined in the
DIV-contained script elements and allows new ones of the same name(s) to be
created.

For example if you had a function called "Init()" in multiple pieces of
content IE would always run the first instance loaded into the DIV UNLESS
you "null" the container before loading the subsequent block.

The "pages" in my applications (all content retrieved via an XMLHttpRequest
call and injected into a DIV ) all reference a created psuedo-scope called
"Page" and have certain standardized functions ("Page.Init()" which
initilizes the page, "Page.Denit()" which run when the page is unloaded and
"Page.Renit()" which is run when the page is resurfaced but not reloaded).

The nulling of the container DIV is neccessary to allow the replacement
functions to be properly added to the model.

Jim Davis
Jul 20 '06 #9

"Jim Davis" <ne********@vboston.comwrote in message
news:MK********************@giganews.com...
>
Oh - one other imprtant thing: if you set the injected script to "defer"
then FireFox will run it as well... but it has other troubles with
overwriting functions.

Still if you just plain want to run an "alert()" like the OP then "defer"
seems to be your friend.

Jim Davis
Jul 20 '06 #10

"Jim Davis" <ne********@vboston.comwrote in message
news:Qd********************@giganews.com...
>
"Jim Davis" <ne********@vboston.comwrote in message
news:MK********************@giganews.com...
>>

Oh - one other imprtant thing: if you set the injected script to "defer"
then FireFox will run it as well... but it has other troubles with
overwriting functions.

Still if you just plain want to run an "alert()" like the OP then "defer"
seems to be your friend.
One last thing (well.. maybe not since you guys got me thinking about this):

Remember that "defer" has other aspects to it. In this arena the main issue
to worry about is that "defer" essentially instructs the agent that this
block of script will not generate inline content (no "document.write()"
statements).

So while it seems that you can use "defer" to inject script into IE and
Firefox (with caveats) you can't use it to inject ALL script. Any inline
code generation will really foul things up in both IE and Firefox (the
entire current document is thrown away and replaced with the newly generated
content).

This isn't at all "wrong" or unexpected if you know what "defer" actually
does but might be a suprise to the unaware.

Jim Davis
Jul 20 '06 #11
Jim Davis wrote:
Jim Davis wrote:
<snip>
>One wrinkle to this.

IE will parse/execute a script block injected into a DIV
- however you must first set the innerHTML property to
"null" THEN load the content as in:

document.getElementById(ElementID).innerHTML = null;
document.getElementById(ElementID).innerHTML = NewContent;

Actually I've fibbed a little. More information:
<snip>
Setting the innerHTML to null is not actually required
I am glad you backtracked on this as assigning null to innerHTML really
looked like 'mystical incantation' programming. In principle the null
would be type-converted into the string 'null', which should have no
special significance when inserted with innerHTM, and if the desire was
to empty the contents of the element prior to assigning new innnerHTML
it would be more reasonable to be assigning an empty string than null.
to run the script however it's still a VERY good idea.
The reason is because IE will not automatically over-write
any existing functions unless you do this. Setting the
innerHTML to null actually unloads all functions defined in
the DIV-contained script elements and allows new ones of the
same name(s) to be created.
<snip>

That doesn't appear to be true when tested in IE 6 with:-

<html>
<head>
<title></title>
<script type="text/javascript">
var count = 0;
function runTest(){
var div = document.getElementById('t1');
div.innerHTML = 'x<script type="text/javascript" defer>'+
'function s(){return '+(++count)+';}<\/script>';
alert(window.s);
}
function runTest2(){
var div = document.getElementById('t1');
div.innerHTML = null;
alert(window.s)

}
</script>
</head>
<body>
<div id="t1"></div>
<input type="button" onclick="runTest();" value="Load Script">
<input type="button" onclick="runTest2();" value="null innerHTML">
</body>
</html>

- where, say, pressing the "Load Script" repeatedly wihtout pressing the
"null innerHTML" button shows the numbr in the disapyed source for the -
s - fucntion incermenting (so each previous version must have been
replaced), and subsequanty pressing the "null innerHTML" button shows no
eveidence of the current - s - fuction being influenced in any way. So
no unloading of "all functions defined in the DIV-contained script
elements".

This suggests that you have acquired a misconception about what is
happening in IE. It might be an idea to try backing future assertions on
this subject up with code that demonstrates the phenomena you assert so
that people can point out the sources of any further misconceptions.

Richard.
Jul 20 '06 #12
Jim Davis said the following on 7/20/2006 11:42 AM:
"Jim Davis" <ne********@vboston.comwrote in message
news:dr******************************@giganews.com ...
>"Randy Webb" <Hi************@aol.comwrote in message
news:3P******************************@comcast.com ...
>>PJ said the following on 7/19/2006 10:43 AM:
Greetings...
*snip*
>>>However the HTML have a SCRIPT tag that the browser should process, but
it doesn't.
Script blocks inserted via innerHTML don't get executed in any browser
other than NS6
One wrinkle to this.

IE will parse/execute a script block injected into a DIV - however you
must first set the innerHTML property to "null" THEN load the content as
in:

document.getElementById(ElementID).innerHTML = null;
document.getElementById(ElementID).innerHTML = NewContent;

Actually I've fibbed a little.
Actually, it was a lot, not a little.
More information:

To get the injected script to actually run in IE you also need to set the
"defer" attribute on the script element to "defer" as in:

<script type="text/javascript" defer="defer">
alert("My Script's a runnin'!");
</script>

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
Setting the innerHTML to null is not actually required to run the script
however it's still a VERY good idea.
No, it's not. As Richard pointed out, but even then it's a moot point.
The reason is because IE will not automatically over-write any
existing functions unless you do this.
I don't believe that.
Setting the innerHTML to null actually unloads all functions defined in the
DIV-contained script elements and allows new ones of the same name(s) to be
created.
I don't believe that either.
For example if you had a function called "Init()" in multiple pieces of
content IE would always run the first instance loaded into the DIV UNLESS
you "null" the container before loading the subsequent block.
That is about as far from true as you can get.
The "pages" in my applications (all content retrieved via an XMLHttpRequest
call and injected into a DIV ) all reference a created psuedo-scope called
"Page" and have certain standardized functions ("Page.Init()" which
initilizes the page, "Page.Denit()" which run when the page is unloaded and
"Page.Renit()" which is run when the page is resurfaced but not reloaded).
The "pages" have something else in them that is screwing it up then. Or,
you have a completely broken version of IE.
The nulling of the container DIV is neccessary to allow the replacement
functions to be properly added to the model.
Only if you have something else causing it. Probably from relying on
some voodoo incantation using innerHTML instead of something more
reliable to trigger your code.

--
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/
Jul 21 '06 #13
Randy Webb wrote:
However the HTML have a SCRIPT tag that the browser should process, but
it doesn't.

Script blocks inserted via innerHTML don't get executed in any browser
other than NS6
Is NS6 likely the reason that Prototype.js has a update() function that
first strips the scripts like shown in the followng snips. Is stripping
the scripts worthwhile as NS6 must be fairly old by now?

ScriptFragment: '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)',
stripScripts: function() {
return this.replace(new RegExp(Prototype.ScriptFragment, 'img'),
'');
},
update: function(element, html) {
$(element).innerHTML = html.stripScripts();
setTimeout(function() {html.evalScripts()}, 10);
},
Why do you think there is a setTimeout() before evaluating the scripts?

Disclaimers:
* I would ask the author if I thought he was a reliable source of
JavaScript coding. I don't so I am appealing to c.l.j. collective
wisdom.

* I don't want to use Prototype.js but I do need to understand it if I
am going to replace parts of it for my use with Rails.

Thanks,
Peter

Jul 21 '06 #14
pe**********@gmail.com said the following on 7/20/2006 9:26 PM:
Randy Webb wrote:
>>However the HTML have a SCRIPT tag that the browser should process, but
it doesn't.
Script blocks inserted via innerHTML don't get executed in any browser
other than NS6

Is NS6 likely the reason that Prototype.js has a update() function that
first strips the scripts like shown in the followng snips.
I highly doubt it.
Is stripping the scripts worthwhile as NS6 must be fairly old by now?
It isn't simply "stripping the scripts". What it should be doing is
reading the text of the script elements and creating new script elements
with the text so that they get executed reliably.
Why do you think there is a setTimeout() before evaluating the scripts?
Probably to keep from locking up the browser on a script block intensive
set of code.
Disclaimers:
* I would ask the author if I thought he was a reliable source of
JavaScript coding. I don't so I am appealing to c.l.j. collective
wisdom.
If you want to know how to cause script blocks to be executed when
inserted in a page via innerHTML, then clj is the best place to ask.
* I don't want to use Prototype.js but I do need to understand it if I
am going to replace parts of it for my use with Rails.
You would do better off by replacing all of it for use with anything.

--
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/
Jul 21 '06 #15
"Richard Cornford" <Ri*****@litotes.demon.co.ukwrote in message
news:e9*******************@news.demon.co.uk...
Jim Davis wrote:
>Jim Davis wrote:
<snip>
>>One wrinkle to this.
>snip<
>to run the script however it's still a VERY good idea.
The reason is because IE will not automatically over-write
any existing functions unless you do this. Setting the
innerHTML to null actually unloads all functions defined in
the DIV-contained script elements and allows new ones of the
same name(s) to be created.
<snip>

That doesn't appear to be true when tested in IE 6 with:-
Yeah... I built a striupped dowqn version to test myself (but can't post
from the office).

However I think I may dig deeper... I pride myself on commenting and it's
clearly commented in the system. But I built the original system in IE 5
which unfortunately I don't have readily available.
This suggests that you have acquired a misconception about what is
happening in IE. It might be an idea to try backing future assertions on
this subject up with code that demonstrates the phenomena you assert so
that people can point out the sources of any further misconceptions.
Misconception acquired, identifed and eliminated.

Jim Davis
Jul 21 '06 #16

"Randy Webb" <Hi************@aol.comwrote in message
news:n7******************************@comcast.com. ..
Jim Davis said the following on 7/20/2006 11:42 AM:
>"Jim Davis" <ne********@vboston.comwrote in message
news:dr******************************@giganews.co m...
>>"Randy Webb" <Hi************@aol.comwrote in message
news:3P******************************@comcast.co m...
PJ said the following on 7/19/2006 10:43 AM:
Greetings...
>snip<
>Actually I've fibbed a little.

Actually, it was a lot, not a little.
Well... I _thought_ it was a little. Turned out it was a lot. ;^)
>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...

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.

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

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]);

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.
>For example if you had a function called "Init()" in multiple pieces of
content IE would always run the first instance loaded into the DIV UNLESS
you "null" the container before loading the subsequent block.

That is about as far from true as you can get.
Well I can get much, MUCH farther from "true" but that's beside the point.
;^)

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.

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.

Oh... one more aspect of this when injecting script into DIVs to simulate
new "pages".

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.

Automatically "knowing" or discovering which declarations were made by the
script blocks in question could be nightmarishly difficult.

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.

Jim Davis
Jul 21 '06 #17
Jim Davis said the following on 7/20/2006 11:56 PM:
"Randy Webb" <Hi************@aol.comwrote in message
news:n7******************************@comcast.com. ..
>Jim Davis said the following on 7/20/2006 11:42 AM:
<snip>
>>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.
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.
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.
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.
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.
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>
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.
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.
Oh... one more aspect of this when injecting script into DIVs to simulate
new "pages".
It's not an "aspect", its a misunderstanding.
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
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.
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/
Jul 21 '06 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: RobG | last post by:
I know you aren't supposed to use innerHTML to mess with table structure, but it seems you can't use it just after a table without damaging the containing element. I added a table to a div using...
4
by: bissatch | last post by:
Hi, I have the following simple HTML page. I am trying to get the innerHTML of the table element, "xmltable". I do intend to change the innerHTML of this table but at this stage I am having...
8
by: McKirahan | last post by:
Firefox does not reflect selected option via innerHTML How do I get Firefox to reflect selected option values? <html> <head> <title>FFinner.htm</title> <script type="text/javascript">...
1
by: Paul | last post by:
Here is my current code: ------------ <script runat=Server> void Page_Load(object sender, EventArgs e){ if (!IsPostBack){ myDiv.InnerHtml = "Original Content"; } //On Postback, should equal...
4
by: robert.waters | last post by:
Hello, When the following page is executed in IE6, it generates script errors ('object expected') for any function that is declared in an external ..js file. Why is this???? This code works in...
2
by: Edwin Knoppert | last post by:
Can this be done? I want to avoid the alert comming back on back and forwards navigation. Hmm, maybe a var (boolean)? (Don't mention the js execution-flow itself, it is presented odd ) <div...
11
by: tlyczko | last post by:
Hello Rob B posted this wonderful code in another thread, http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/c84d8538025980dd/6ead9d5e61be85f0#6ead9d5e61be85f0 I could not...
3
sabesneit
by: sabesneit | last post by:
Hi, I'm tring to execute some javascript with innerHTML but nothing happen, here a simplified example: <html> <head> <title>this doesnt work!</title> <script language="javascript"> ...
2
by: Tereska | last post by:
I want to delete script added before. I'm adding script dynamically and i'm removing later. Why it is still working? I have something like this: <html> <head> <title>JS Script...
1
by: IntoEternity | last post by:
Hello, I found a script for innerHTML, which has a basic expand / collapse function. However, when it comes to table cells … it craps out. I know some form of toggle function needs to be added,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.