![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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... |
#3
| |||
| |||
|
|
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. |
#4
| |||
| |||
|
|
On Aug 20, 2:20 am, "Jon Maz" <jon... (AT) surfeu (DOT) deno.spam> wrote: [...] 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 are not 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. Peter |
#5
| |||
| |||
|
|
On Mon, 20 Aug 2007 15:55:50 +0000, Peter Michaux wrote: On Aug 20, 2:20 am, "Jon Maz" <jon... (AT) surfeu (DOT) deno.spam> wrote: [...] 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 ( do not remember if it is, but I recall there beeing a way to reference it. ) I would write eval( "function() {" + THE_CODE_FROM_THE_SCRIPT_TAG + "}" ).apply( window, [] ) ; to get correct scope. |
#6
| |||
| |||
|
|
On Mon, 20 Aug 2007 15:55:50 +0000, Peter Michaux wrote: [...] 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 ( do not remember if it is, but I recall there beeing a way to reference it. ) I would write eval( "function() {" + THE_CODE_FROM_THE_SCRIPT_TAG + "}" ).apply( window, [] ) ; to get correct scope. |
|
This should fix your scope. snip |
#7
| |||
| |||
|
|
Thomas M. Farrelly wrote: On Mon, 20 Aug 2007 15:55:50 +0000, Peter Michaux wrote: [...] 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. This statement is, of course, false. Because the eval execution context uses the variable object from the execution context in which it is executed any functions or variables it declares must persist after it finishes executing. They persist for precisely as long as that Variable object persists. |
#8
| |||
| |||
|
|
On Aug 20, 2:20 am, "Jon Maz" <jon... (AT) surfeu (DOT) deno.spam> wrote: 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... I wrote 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 variables 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 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 are not 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. |
#9
| |||
| |||
|
|
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 haven't looked at it). |
|
If you are not 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. |
#10
| |||
| |||
|
|
Hi Peter, Thanks for that - I had no idea this would 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. |
|
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... |
![]() |
| Thread Tools | |
| Display Modes | |
| |