How to make injected js execute?
68 Message(s) by 9 Author(s) originally posted in javascript
| From: Jon Maz |
Date: Sunday, August 19, 2007
|
Hi,
I have got a simple
bit of
code that successfully injects a JAVAscript
alert
into the
DOM (confirmed by View Generated Source in
Firefox ), yet that alert
isn't
execute d.
QUESTION: Why does not it execute?
QUESTION: Can I make it execute?
<html>
<body>
<div id="myDiv"></div>
<script>
var myDiv =
document .getElementById("myDiv");
myDiv.innerHTML = "\
<script\>alert('
foo ');\
</script\>";
</script>
</body>
</html>
Thanks in advance,
JON
| From: Peter Michaux |
Date: Sunday, August 19, 2007
|
wrote in
message :
Hi,
I have got a simple bit of code that successfully injects a JAVAscript alert
into the DOM (confirmed by View Generated Source in Firefox), yet that alert
isn't executed.
QUESTION: Why does not it execute?
QUESTION: Can I make it execute?
<html>
<body>
<div id="myDiv"></div>
<script>
var myDiv = document.getElementById("myDiv");
myDiv.innerHTML = "\<script\>alert('foo');\</script\>";
</script>
</body>
</html>
Thanks in advance,
JON
This issue is not as easy as it seems it should be. Randy Webb has
looked into this in great detail. There was a recent
thread ...
<URL :
http://group s.google.com/group/comp.lang.JAVAscript/browse_frm/thread/fcb50a7c0a2c7fab/9adffa2527da0f51?lnk=gst&q=script+insertion+revisited&rnum=1#9adffa2527da0f51>
Peter
| From: Jon Maz |
Date: Monday, August 20, 2007
|
Hi Peter,
Thanks for that - I had no idea this'd be a complex issue. A couple of
observations:
1. for (possibly demented) reasons of my own it's actually an arbitrary mix
of js and html that I want to inject into the page, so of the 5
method s
offered in the other thread:
* Change Source- attempts to change the .src property of a script
element .
* Change innerHMTL - inserts a script
string via innerHTML.
* createElement - uses createElement to create a script
block with a .src
attribute .
* Change .
text - changes the .text property of a script element.
* createTextNode - creates the text part of a script element using
createTextNode
... I think only the .innerHTML one is an
option for me.
2. A lot of the thread you referred me to is about IE, whereas my observed
issue affects Firefox too.
My fingers are still crossed that there's an answer to this for me, but hope
is ebbing away fast...
Cheers,
JON
| From: Peter Michaux |
Date: Monday, August 20, 2007
|
wrote in message:
1. for (possibly demented) reasons of my own it's actually an arbitrary mix
of js and html that I want to inject into the page, so of the 5 methods
offered in the other thread:
* Change Source- attempts to change the .src property of a script element.
* Change innerHMTL - inserts a script string via innerHTML.
* createElement - uses createElement to create a script block with a .src
attribute.
* Change .text - changes the .text property of a script element.
* createTextNode - creates the text part of a script element using
createTextNode
... I think only the .innerHTML one is an option for me.
2. A lot of the thread you referred me to is about IE, whereas my observed
issue affects Firefox too.
My fingers are still crossed that there's an answer to this for me, but hope
wrote in message a library that does what you want to do...sort of.
<URL:
http://forkJAVAscript.org/mutate/docs>
What my script does it scan through the
HTML that is about to be
inserted and pluck out all the script bits. It inserts the other HTML
and then
process the JAVAScript bits. Instead of dynamic script
insertion, as Randy has investigated, I just eval() the script bits.
This is more cross-
browser but has scope implications.
If you are in
control of writing the JAVAScript bits then you can just
write the JAVAScript bits so there are no problems with using eval().
There is only one little complication and it is with naming global
variable s explicitly as global properties instead. Here is the
pertinent part of the above page regarding this issue.
<blockquote>
The main issue is the scope of entities (eg. variables or
function s)
defined in the evaluated script. Because the call to eval() occurs
inside a JAVAScript function, entities defined in the evaluated script
may not persist after eval() returns. If you do want to define global
entities you may look at writing something like the following snip
inside the code block.
window.foo = 2; // instead of var foo = 2
</blockquote>
If you aren't in charge of the script bits and want to use script
insertion you could combine my library and one of the
solution s posted
in the other c.l.j thread about script insertion.
Peter
| From: Peter Michaux |
Date: Monday, August 20, 2007
|
wrote in message:
wrote in message:
wrote in message:
[...]
>
<blockquote>
> The main issue is the scope of entities (eg. variables or functions)
> defined in the evaluated script. Because the call to eval() occurs
> inside a JAVAScript function, entities defined in the evaluated script
> may not persist after eval() returns. If you do want to define global
> entities you may look at writing something like the following snip
> inside the code block.
if window is the
root scope ( don't remember if it is, but I recall there
beeing a way to
reference it. ) I'd write
eval( "function() {"
+ THE_CODE_FROM_THE_
SCRIPT _TAG +
"}" ).apply( window, [] ) ;
to get correct scope.
A function's apply() will set the scope the "this"
key word when the
function
run s. There is more to the script insertion problem then just
resolving "this". Here is an example where the script to be inserted
is "var foo=1;"
var foo=4;
eval( "function() {var foo=1;}" ).apply( window, [] ) ;
alert(foo);
In this case the alert says "4" but successful script insertion, as
has been defined and discussed,'d result in an alert of "1". The
goal is to have the script run in the global scope
complete ly.
Peter
| From: Randy Webb |
Date: Monday, August 20, 2007
|
Peter Michaux said the following on 8/20/2007 11:55 AM:
wrote in message:
1. for (possibly demented) reasons of my own it's actually an arbitrary mix
of js and html that I want to inject into the page, so of the 5 methods
offered in the other thread:
* Change Source- attempts to change the .src property of a script element.
* Change innerHMTL - inserts a script string via innerHTML.
* createElement - uses createElement to create a script block with a .src
attribute.
* Change .text - changes the .text property of a script element.
* createTextNode - creates the text part of a script element using
createTextNode
... I think only the .innerHTML one is an option for me.
2. A lot of the thread you referred me to is about IE, whereas my observed
issue affects Firefox too.
My fingers are still crossed that there's an answer to this for me, but hope
is ebbing away fast...
wrote in message a library that does what you want to do...sort of.
<URL: http://forkJAVAscript.org/mutate/docs>
What my script does it scan through the HTML that is about to be
inserted and pluck out all the script bits. It inserts the other HTML
and then process the JAVAScript bits. Instead of dynamic script
insertion, as Randy has investigated, I just eval() the script bits.
This is more cross-browser but has scope implications.
I have a script somewhere that lets you simply insert it via innerHTML
and then reads the script blocks back out and executes them. Is that
what yours does? (I have not looked at it).
If you are in control of writing the JAVAScript bits then you can just
write the JAVAScript bits so there are no problems with using eval().
There is only one little complication and it is with naming global
variables explicitly as global properties instead. Here is the
pertinent part of the above page regarding this issue.
The other problem that can come up is if your scripts contain a
document.write
state ment :-)
<blockquote>
The main issue is the scope of entities (eg. variables or functions)
defined in the evaluated script. Because the call to eval() occurs
inside a JAVAScript function, entities defined in the evaluated script
may not persist after eval() returns. If you do want to define global
entities you may look at writing something like the following snip
inside the code block.
window.foo = 2; // instead of var foo = 2
</blockquote>
If you aren't in charge of the script bits and want to use script
insertion you could combine my library and one of the solutions posted
in the other c.l.j thread about script insertion.
I have yet to see a solution that
handle s document.write but I have an
idea in mind for it.
--
Randy
Chance Favors The Prepared Mind
comp.lang.JAVAscript
FAQ -
http://jibbering.com/faq/index.html
JAVAscript Best Practices -
http://www.JAVAscriptToolbox.com/bestpractices/
| From: Peter Michaux |
Date: Monday, August 20, 2007
|
wrote in message:
Peter Michaux said the following on 8/20/2007 11:55 AM:
> <URL:http://forkJAVAscript.org/mutate/docs>
> What my script does it scan through the HTML that is about to be
> inserted and pluck out all the script bits. It inserts the other HTML
> and then process the JAVAScript bits. Instead of dynamic script
> insertion, as Randy has investigated, I just eval() the script bits.
> This is more cross-browser but has scope implications.
I have a script somewhere that lets you simply insert it via innerHTML
and then reads the script blocks back out and executes them. Is that
what yours does? (I have not looked at it).
I extract the scripts from the HTML before inserting the HTML into the
page. That way if some browser starts to automatically evaluate
scripts when they are inserted by innerHTML I'm not screwed. There
already is one old browser out there that does this: NN6? Then after
inserting the HTML, I eval() the script bits in order. There are pros
and cons for just about every decisions in dealing with this stuff. I
find the
system I built easy to use.
There is a bit more too it then that. My script takes into account
problems with inserting
table rows or tbody elements by "parsing" the
html first in the appropriate surrounding element. There are
assumptions about a well formed html fragment. blah blah blah. That
script was actually a fun project.> > If you aren't in charge of the script bits and want to use script
> insertion you could combine my library and one of the solutions posted
> in the other c.l.j thread about script insertion.
I have yet to see a solution that handles document.write but I have an
idea in mind for it.
You are obsessed! Just curious but when is it that you are wanting to
do script insertion with totally
random scripts you have not written
yourself or written by a coworker? Just academic interest?
Peter
| From: Randy Webb |
Date: Monday, August 20, 2007
|
Jon Maz said the following on 8/20/2007 5:20 AM:
Hi Peter,
Thanks for that - I had no idea this'd be a complex issue. A couple of
observations:
1. for (possibly demented) reasons of my own it's actually an arbitrary mix
of js and html that I want to inject into the page, so of the 5 methods
offered in the other thread:
* Change Source- attempts to change the .src property of a script element.
* Change innerHMTL - inserts a script string via innerHTML.
* createElement - uses createElement to create a script block with a .src
attribute.
* Change .text - changes the .text property of a script element.
* createTextNode - creates the text part of a script element using
createTextNode
... I think only the .innerHTML one is an option for me.
The thread that Peter sent you to is not the best thread for what you are
trying to do. There is another one in the archives if I can find it.
<URL:
http://groups.google.com/group/comp.lang.JAVAscript/search?group=comp.lang.JAVAscript&q=load HTMLFragment>
The name of the function that was last written for what you are doing is
named loadHTMLFragment and looks like this:
function loadHTMLFragment(elemId, HTMLFragment)
{
if (document &&
document.getElementById &&
document.getElementById(elemId) &&
document.createElement &&
document.appendChild &&
document.getElementsByTagName)
{
var el = document.getElementById(elemId);
el.innerHTML = " " + HTMLFragment;
//The is a
hack to cause IE to process the
//script elements if the first
node in the
//HTMLFragment is a script element.
var d =el.getElementsByTagName('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;
el.appendChild(newScript);
}
for (var y=0;y<t;y++)
{
el.removeChild(el.getElementsByTagName("script")[y]);
}
}
}
The
comment you see about IE is because there is a
bug in IE where if
the first element in the innerHTML is a script element then it won't
execute that script block for you for some odd ball reason. The simplest
solution was to blanket insert a blank
space and avoid the issue all
together.
It still has issues with potential scope issues, and with document.write
issues. If any of your script blocks have a document.write in it then it
will fubar your page by replacing it. I have some ideas on ways around
it but have not fooled with it in a few months. Maybe this will
push me
to get around to it. The basic idea is to scan the script blocks, find
document.write
line s and try to figure out how to go about inserting
whatever it was trying to write. Even that has issues with it.
2. A lot of the thread you referred me to is about IE, whereas my observed
issue affects Firefox too.
Your issue affects any browser other than NS6.1x, NS6.2x, iCab3.0.3 Mac,
IE5.2 Mac as they are the only ones that will execute scripts inserted
via innerHTML (unless there is one that I do not know about). If you find
a browser other than those above that will execute scripts inserted via
innerHTML, please let me know.
My fingers are still crossed that there's an answer to this for me, but hope
is ebbing away fast...
There is an answer, and your hope can ebb higher. Search the archives
for - script insertion randy webb - and you can find more information
about what you are trying to do than I care to
sit and type out again.
The biggest thing is whether you control (in any manner) the HTML/Script
you are inserting into your page. If you do, and plan it properly,
script insertion becomes almost trivial.
--
Randy
Chance Favors The Prepared Mind
comp.lang.JAVAscript FAQ -
http://jibbering.com/faq/index.html
JAVAscript Best Practices -
http://www.JAVAscriptToolbox.com/bestpractices/
| From: Randy Webb |
Date: Monday, August 20, 2007
|
Peter Michaux said the following on 8/20/2007 8:43 PM:
wrote in message:
Peter Michaux said the following on 8/20/2007 11:55 AM:
<URL:http://forkJAVAscript.org/mutate/docs>
What my script does it scan through the HTML that is about to be
inserted and pluck out all the script bits. It inserts the other HTML
and then process the JAVAScript bits. Instead of dynamic script
insertion, as Randy has investigated, I just eval() the script bits.
This is more cross-browser but has scope implications.
I have a script somewhere that lets you simply insert it via innerHTML
and then reads the script blocks back out and executes them. Is that
what yours does? (I have not looked at it).
I extract the scripts from the HTML before inserting the HTML into the
page. That way if some browser starts to automatically evaluate
scripts when they are inserted by innerHTML I'm not screwed. There
already is one old browser out there that does this: NN6? Then after
inserting the HTML, I eval() the script bits in order. There are pros
and cons for just about every decisions in dealing with this stuff. I
find the system I built easy to use.
NN6.1, NN6.2, iCab3 and IE5.2/Mac execute scripts inserted via
innerHMTL. Detecting that behavior is trivial though. Use the onload
event, insert something like this via innerHTML:
<script type="text/JAVAscript">
someBooleanVariable = false;
</script>
And have this already in the page:
<script type="text/JAVAscript">
someBooleanVariable = true;
</script>
And then have your function check that variable:
function doSomethingWithThatAwfulCode(){
if(someBooleanVariable){return}
}
Then, if they get executed previously you know not to fool with them
again, you simply return out of the function.
There is a bit more too it then that. My script takes into account
problems with inserting table rows or tbody elements by "parsing" the
html first in the appropriate surrounding element. There are
assumptions about a well formed html fragment. blah blah blah. That
script was actually a fun project.
I have never been one to try to dynamically create tables on the fly.
Most of my interest in it is the fact that IE gets it right and FF gets
it wrong. The personal page I was working on when I first started
playing with HikkScript (JS on the fly) used tables but I have not fooled
with it in about 5 years or so. Can you imagine trying to reverse a
script that document.writes embedded tables though?
Note: I have decided to start calling this HikkScript just for the sake
of it and make it easier to find in the archives. It has a certain
"personal ring" to it :)
If you aren't in charge of the script bits and want to use script
insertion you could combine my library and one of the solutions posted
in the other c.l.j thread about script insertion.
I have yet to see a solution that handles document.write but I have an
idea in mind for it.
You are obsessed!
That's what they tell me. A "This attitude plus FAQ mantainer: a true
humanitarian!" is what one poster here said about me when I said I
wanted to be ahead of the game on this one :) (You can find that in the
createTextNode IE7 thread).
Just curious but when is it that you are wanting to
do script insertion with totally random scripts you have not written
yourself or written by a coworker?
Personally? I do not. It comes up mostly from ill-fated "Ajax sites"
where people do not re-do the back end to make it ajax friendly and just
retrieve complete pages and want to insert it. Then they can say "Yeah,
we use AJAX on our site" without taking advantage of the main advantage
of AJAX.
Just academic interest?
Mostly, as anybody that is trying to insert HTML with document.writes in
it has more problems than trying to get the document.write to work right
as they need to change the back end to get rid of the document.write
statements.
--
Randy
Chance Favors The Prepared Mind
comp.lang.JAVAscript FAQ -
http://jibbering.com/faq/index.html
JAVAscript Best Practices -
http://www.JAVAscriptToolbox.com/bestpractices/
| From: Randy Webb |
Date: Monday, August 20, 2007
|
Randy Webb said the following on 8/20/2007 8:46 PM:
Jon Maz said the following on 8/20/2007 5:20 AM:
Hi Peter,
Thanks for that - I had no idea this'd be a complex issue. A
couple of observations:
1. for (possibly demented) reasons of my own it's actually an
arbitrary mix of js and html that I want to inject into the page, so
of the 5 methods offered in the other thread:
* Change Source- attempts to change the .src property of a script
element.
* Change innerHMTL - inserts a script string via innerHTML.
* createElement - uses createElement to create a script block with a
.src attribute.
* Change .text - changes the .text property of a script element.
* createTextNode - creates the text part of a script element using
createTextNode
... I think only the .innerHTML one is an option for me.
The thread that Peter sent you to is not the best thread for what you are
trying to do. There is another one in the archives if I can find it.
<URL:
http://groups.google.com/group/comp.lang.JAVAscript/search?group=comp.lang.JAVAscript&q=loadHTMLFragment>
The name of the function that was last written for what you are doing is
named loadHTMLFragment and looks like this:
I have revised that function to accommodate NN6.1/NN6.2Win and hopefully
it'll accommodate the innerHTML mac browsers (iCab and IE). If anyone
could test it on a non-windows browser I'd be grateful. Added was
the innerHTML
testing so that browsers that execute innerHTML scripts
would not execute them twice.
var innerHTMLFailed=true;
window.onload = checkIt;
function checkIt(){
document.getElementById('myDiv').innerHTML = '
<script
type="text/JAVAscript">var innerHTMLFailed = false;
<\/script>'
}
function loadHTMLFragment(elemId, HTMLFragment)
{
if (document &&
document.getElementById &&
document.getElementById(elemId) &&
document.createElement &&
document.appendChild &&
document.getElementsByTagName &&
{
var el = document.getElementById(elemId);
el.innerHTML = " " + HTMLFragment;
//The is a hack to cause IE to process the
//script elements if the first node in the
//HTMLFragment is a script element.
if(innerHTMLFailed)
{
var d =el.getElementsByTagName('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;
el.appendChild(newScript);
}
for (var y=0;y<t;y++)
{
el.removeChild(el.getElementsByTagName("script")[y]);
}
}
}
}
Next step is a test of the .text attribute since I know of browsers that
don't
support setting the .text property of a newScript element.
--
Randy
Chance Favors The Prepared Mind
comp.lang.JAVAscript FAQ -
http://jibbering.com/faq/index.html
JAVAscript Best Practices -
http://www.JAVAscriptToolbox.com/bestpractices/
| From: Jon Maz |
Date: Tuesday, August 21, 2007
|
<quote>
You are obsessed! Just curious but when is it that you are wanting to
do script insertion with totally random scripts you have not written
yourself or written by a coworker? Just academic interest?
</quote>
For myself I am trying to write code that asynchronously injects adverts
into a web page. You will notice across the web millions of sites whose pages
might be quite fast-loading in themselves, but with 3 or 4 massive delays as
the page goes to ads.domain.com to fetch the ad code, which
characteristically is a mix of JAVAscript and html, and is
generate d by 3rd
party software.
I want this external
data injected in the
background , after the main page
has finished rendering, for an improved user experience. Make sense?
JON
| From: Jon Maz |
Date: Tuesday, August 21, 2007
|
| From: Peter Michaux |
Date: Tuesday, August 21, 2007
|
wrote in message:
Hi Peter,
Currentlyhttp://forkJAVAscript.org/mutate/docsis down. A temporary issue?
Unfortunately it is down. I'm trying to get my hosting company to tell
me what's up.
Hope fully it'll be back up today.
Peter
| From: Peter Michaux |
Date: Tuesday, August 21, 2007
|
wrote in message:
<quote>
You are obsessed! Just curious but when is it that you are wanting to
do script insertion with totally random scripts you have not written
yourself or written by a coworker? Just academic interest?
</quote>
For myself I am trying to write code that asynchronously injects adverts
into a web page. You will notice across the web millions of sites whose pages
might be quite fast-loading in themselves, but with 3 or 4 massive delays as
the page goes to ads.domain.com to fetch the ad code, which
characteristically is a mix of JAVAscript and html, and is generated by 3rd
party software.
I want this external data injected in the background, after the main page
has finished rendering, for an improved user experience. Make sense?
What about just inserting an iframe into the page and setting the src
attribute to a URL that produces the ads? Because of iframes, it is not
a valid approach for HTML
strict pages but is on HTML transitional.
Peter
| From: Jon Maz |
Date: Tuesday, August 21, 2007
|
Hi Peter,
<quote>
What about just inserting an iframe into the page and setting the src
attribute to a URL that produces the ads? Because of iframes, it is not
a valid approach for HTML strict pages but is on HTML transitional.
</quote>
That's probably where I will end up. For reasons I can not remember now I
started trying to do this the .js way, and I'm stupidly determined to try to
get it to work.
Though in fact I am going for HTML strict too <sigh>.
JON
| From: Peter Michaux |
Date: Tuesday, August 21, 2007
|
wrote in message:
Hi Peter,
<quote>
What about just inserting an iframe into the page and setting the src
attribute to a URL that produces the ads? Because of iframes, it is not
a valid approach for HTML strict pages but is on HTML transitional.
</quote>
That's probably where I will end up. For reasons I can not remember now I
started trying to do this the .js way, and I'm stupidly determined to try to
get it to work.
Though in fact I am going for HTML strict too <sigh>.
I tried that for a while. Now I think HTML transitional is the only
useful doctype. For IE6 if you want to do overlays you need the
"iframe
shim hack". If you want to do background
file uploads that
look like XMLHttpRequest to the user then you need hidden iframes.
If you'll be allowing arbitrary people to inject arbitrary HTML in
your page there is a better chance things will work out well if you
are using the more forgiving transitional doctype.
Peter
| From: Randy Webb |
Date: Tuesday, August 21, 2007
|
Jon Maz said the following on 8/21/2007 6:23 AM:
<quote>
You are obsessed! Just curious but when is it that you are wanting to
do script insertion with totally random scripts you have not written
yourself or written by a coworker? Just academic interest?
</quote>
For myself I am trying to write code that asynchronously injects adverts
into a web page. You will notice across the web millions of sites whose pages
might be quite fast-loading in themselves, but with 3 or 4 massive delays as
the page goes to ads.domain.com to fetch the ad code, which
characteristically is a mix of JAVAscript and html, and is generated by 3rd
party software.
If the JS has any document.write calls, "with" operators, refers to
"this" and there are possibly a few more that I can not think of off the
top of my head, then you are
dead in the water before you start. You
would just about have to write your own HTML
parser to determine
what/where to insert elements into the DOM and then your page loads
slower because it is having to parse all of the potential elements that
could be inserted with document.write code.
What you are describing is almost exactly the scenario I had in mind
with document.write calls albeit your situation is different than what I
had in mind. If a site uses "AJAX" to retrieve full documents instead of
HTML fragments then you end up with the same problem if the document has
ads/code in it that uses document.write statements.
I want this external data injected in the background, after the main page
has finished rendering, for an improved user experience. Make sense?
Put an IFrame on the page that has a secondary page in it that loads the
ad. It'll make your life simpler and save you a lot of sleepless nights.
--
Randy
Chance Favors The Prepared Mind
comp.lang.JAVAscript FAQ -
http://jibbering.com/faq/index.html
JAVAscript Best Practices -
http://www.JAVAscriptToolbox.com/bestpractices/
| From: pl |
Date: Tuesday, August 21, 2007
|
wrote in message:
> I want this external data injected in the background, after the main page
> has finished rendering, for an improved user experience. Make sense?
Put an IFrame on the page that has a secondary page in it that loads the
ad. It'll make your life simpler and save you a lot of sleepless nights.
Agreed, use an iframe and save yourself from a "world of pain." Some
browsers will let you write the ads normally/directly into the bottom
of the html as the page loads and then move the resulting DOM nodes to
the correct
location after the JS has executed. This is not
real ly
reliable though, particularly given that some ads do their own DOM
manipulation as they load. Whilst Firefox seems to literally move
things with appendChild, IE's implementation seems to be a bit quirky
- some rough testing indicates that it'll leave some
object s behind.
| From: Peter Michaux |
Date: Tuesday, August 21, 2007
|
wrote in message:
In comp.lang.JAVAscript message <1187625350.498917.232...@xxxxxxxxxxx
oglegroups.com>, Mon, 20 Aug 2007 15:55:50, Peter Michaux
<petermich...@xxxxxxxxxxx> posted:
>If you are in control of writing the JAVAScript bits then you can just
>write the JAVAScript bits so there are no problems with using eval().
>There is only one little complication and it is with naming global
>variables explicitly as global properties instead. Here is the
>pertinent part of the above page regarding this issue.
Do you see any future problem in
<URL:http://www.merlyn.demon.co.uk/estrdate.htm#TT>?
One enters code in the blue bit just above heading "Testing and Timing"
(the textarea is preloaded with a function body, but JAVAscript doesn't
know that on loading, so use DEFINE & UNDEFINE), and executes it (check
the checkbox) in the next blue bit.
It's OK in IE6, FF2, Opera9 (though FF has a visible layout flaw).
It may have been a joke but
Thomas appeared to take offense. Did you
apologize for this?
<URL: http://groups.google.com/group/comp.lang.JAVAscript/msg/76e12383ad15a88b?dmode=source >
Peter
| From: Peter Michaux |
Date: Tuesday, August 21, 2007
|
wrote in message:
Randy Webb said the following on 8/20/2007 8:46 PM:
I have revised that function to accommodate NN6.1/NN6.2Win and hopefully
it'll accommodate the innerHTML mac browsers (iCab and IE). If anyone
could test it on a non-windows browser I'd be grateful.
Have you posted a test page? I will
click some buttons for you and send
you the results.
Peter
Added was
the innerHTML testing so that browsers that execute innerHTML scripts
would not execute them twice.
var innerHTMLFailed=true;
window.onload = checkIt;
function checkIt(){
document.getElementById('myDiv').innerHTML = '<script
type="text/JAVAscript">var innerHTMLFailed = false;<\/script>'}
function loadHTMLFragment(elemId, HTMLFragment)
{
if (document &&
document.getElementById &&
document.getElementById(elemId) &&
document.createElement &&
document.appendChild &&
document.getElementsByTagName &&
{
var el = document.getElementById(elemId);
el.innerHTML = " " + HTMLFragment;
//The is a hack to cause IE to process the
//script elements if the first node in the
//HTMLFragment is a script element.
if(innerHTMLFailed)
{
var d =el.getElementsByTagName('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;
el.appendChild(newScript);
}
for (var y=0;y<t;y++)
{
el.removeChild(el.getElementsByTagName("script")[y]);
}
}
}
}
Next step is a test of the .text attribute since I know of browsers that
> don't support setting the .text property of a newScript element.
| From: Randy Webb |
Date: Wednesday, August 22, 2007
|
pl said the following on 8/21/2007 7:37 PM:
wrote in message:
I want this external data injected in the background, after the main page
has finished rendering, for an improved user experience. Make sense?
Put an IFrame on the page that has a secondary page in it that loads the
ad. It'll make your life simpler and save you a lot of sleepless nights.
Agreed, use an iframe and save yourself from a "world of pain."
If you truly want to be free from a "world of pain" then you stop
placing ads on a
website and your problems disappear.
--
Randy
Chance Favors The Prepared Mind
comp.lang.JAVAscript FAQ -
http://jibbering.com/faq/index.html
JAVAscript Best Practices -
http://www.JAVAscriptToolbox.com/bestpractices/
| From: Randy Webb |
Date: Wednesday, August 22, 2007
|
Peter Michaux said the following on 8/21/2007 10:27 PM:
wrote in message:
Randy Webb said the following on 8/20/2007 8:46 PM:
I have revised that function to accommodate NN6.1/NN6.2Win and hopefully
it'll accommodate the innerHTML mac browsers (iCab and IE). If anyone
could test it on a non-windows browser I'd be grateful.
Have you posted a test page? I will click some buttons for you and send
you the results.
Not really. The only thing I was wanting to test was the snippet with
the innerHTML in it to see if IE5.2 and iCab'd reset the variable. A
simple test is the same one I used in NS6:
innerHTMLFailed = true;
window.onload=testIt;
function testIt(){
insertScript()
alert('innerHTMLFailed is ' + innerHTMLFailed)
}
function insertScript(){
document.getElementById('scriptDiv').innerHTML='
<script
type="text/JAVAscript">innerHTMLFailed = false;
<\/script>'
}
If IE5.2 and iCab will alert false then it'll tell me, without testing
the main function, that it'll succeed.
--
Randy
Chance Favors The Prepared Mind
comp.lang.JAVAscript FAQ -
http://jibbering.com/faq/index.html
JAVAscript Best Practices -
http://www.JAVAscriptToolbox.com/bestpractices/
| From: Peter Michaux |
Date: Wednesday, August 22, 2007
|
Hi,
After all this fuss about script insertion and getting scripts to
execute in global scope, I found myself looking through the jQuery
code.
------------------------
// Evalulates a script in a global
context
// Evaluates Async. in Safari 2 :-(
globalEval: function( data ) {
data = jQuery.trim( data );
if ( data ) {
if ( window.execScript )
window.execScript( data );
else if ( jQuery.browser.safari )
// safari does not provide a
synchronous global eval
window.setTimeout( data, 0 );
else
eval.call( window, data );
}
},
------------------------
That window.setTimeout(data, 0) call seems like an easy solution to
the scope problem. I may have missed it but was this every suggested
here instead of all this script element insertion?
Peter
| From: Randy Webb |
Date: Wednesday, August 22, 2007
|
Peter Michaux said the following on 8/22/2007 5:13 PM:
Hi,
After all this fuss about script insertion and getting scripts to
execute in global scope, I found myself looking through the jQuery
code.
------------------------
// Evalulates a script in a global context
// Evaluates Async. in Safari 2 :-(
globalEval: function( data ) {
data = jQuery.trim( data );
if ( data ) {
if ( window.execScript )
window.execScript( data );
else if ( jQuery.browser.safari )
Let me guess, silently, on what that line above does :)
// safari does not provide a synchronous global eval
window.setTimeout( data, 0 );
else
eval.call( window, data );
}
},
------------------------
That window.setTimeout(data, 0) call seems like an easy solution to
the scope problem. I may have missed it but was this every suggested
here instead of all this script element insertion?
I have not personally seen that suggestion and I am not sure what
setTimeout(data, 0) would've to do with any non-Safari browser. And, I
just plain *HATE* eval. Call me bonkers, I just prefer not to use it if
there is a non-eval alternative.
And, what does jQuery.trim do? I can pretty well guess what
jQuery.browser does.
--
Randy
Chance Favors The Prepared Mind
comp.lang.JAVAscript FAQ -
http://jibbering.com/faq/index.html
JAVAscript Best Practices -
http://www.JAVAscriptToolbox.com/bestpractices/
| From: Peter Michaux |
Date: Wednesday, August 22, 2007
|
wrote in message:
Peter Michaux said the following on 8/22/2007 5:13 PM:
> Hi,
> After all this fuss about script insertion and getting scripts to
> execute in global scope, I found myself looking through the jQuery
> code.
> ------------------------
>// Evalulates a script in a global context
>// Evaluates Async. in Safari 2 :-(
>globalEval: function( data ) {
> data = jQuery.trim( data );
> if ( data ) {
> if ( window.execScript )
> window.execScript( data );
> else if ( jQuery.browser.safari )
Let me guess, silently, on what that line above does :)
> // safari does not provide a synchronous global eval
> window.setTimeout( data, 0 );
> else
> eval.call( window, data );
> }
> },
> ------------------------
> That window.setTimeout(data, 0) call seems like an easy solution to
> the scope problem. I may have missed it but was this every suggested
> here instead of all this script element insertion?
I have not personally seen that suggestion
I had not seen it either but I think it is simple and clever.
and I am not sure what
setTimeout(data, 0) would've to do with any non-Safari browser. And, I
just plain *HATE* eval. Call me bonkers, I just prefer not to use it if
there is a non-eval alternative.
The big issue with eval is that it is slow and the code can not be
optimized. Each time eval is used the string has to be parsed first by
the JAVAScript
engine etc. However when you are inserting a script
element into a page you are just inducing eval without calling it. It
is eval in disguise with a global scope. And it also has many cross
browser issues that we've been exploring at length for months with
no real solution. The jQuery solution is extremely
robust and compact.
It looks like a winner in this global-scoped eval issue. If you keep
fighting with that script insertion, and the jQuery technique does not
have any associated problems, then I'm going to call you bonkers on
this one. It's what you have been looking for, is not it?
And, what does jQuery.trim do? I can pretty well guess what
jQuery.browser does.
I do not know jQuery much.
Peter
| From: Randy Webb |
Date: Thursday, August 23, 2007
|
Peter Michaux said the following on 8/22/2007 6:18 PM:
wrote in message:
<snip>
and I am not sure what
setTimeout(data, 0) would've to do with any non-Safari browser. And, I
just plain *HATE* eval. Call me bonkers, I just prefer not to use it if
there is a non-eval alternative.
The big issue with eval is that it is slow and the code can not be
op