![]() | |
![]() |
| | 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
| |||
| |||
|
|
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. |
#4
| |||
| |||
|
|
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. |
#5
| |||
| |||
|
|
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... |
#6
| |||||
| |||||
|
|
On Aug 20, 5:22 pm, Randy Webb <HikksNotAtH... (AT) aol (DOT) com> wrote: 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). 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 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. You are obsessed! |
(You can find that in the|
Just curious but when is it that you are wanting to do script insertion with totally random scripts you haven't written yourself or written by a coworker? |
|
Just academic interest? |
#7
| |||
| |||
|
|
Jon Maz said the following on 8/20/2007 5:20 AM: 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. The thread that Peter sent you to isn't 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....HTMLFragme nt The name of the function that was last written for what you are doing is named loadHTMLFragment and looks like this: |
#8
| |||
| |||
|
#9
| |||
| |||
|
#10
| |||
| |||
|
|
Hi Peter, Currentlyhttp://forkjavascript.org/mutate/docsis down. A temporary issue? |
![]() |
| Thread Tools | |
| Display Modes | |
| |