![]() | |
![]() |
| | Thread Tools | Display Modes |
#31
| ||||
| ||||
|
|
Peter Michaux wrote: |
|
if ( data ) { if ( window.execScript ) window.execScript( data ); else if ( jQuery.browser.safari ) // safari doesn't provide a synchronous global eval window.setTimeout( data, 0 ); else eval.call( window, data ); We have already seen the setting the - this - value is not sufficient for this issue, and that is what the - call - method does. In this case the code appears to be trying to use the deprecated feature of JavaScript(tm) where each object has an - eval - method, though even executing - eval - as a method of an object may be expected to do no more than influence the value of - this -. |
|
} }, ------------------------ That window.setTimeout(data, 0) call seems like an easy solution to the scope problem. The easiest solution is not to design a system where the problem exists. |
|
I may have missed it but was this every suggested here instead of all this script element insertion? It has been suggested several times, a very long time ago and only out of an academic desire for completeness of explanation. Ultimately the issue does not exist. It is trivial for server-client interactions to facilitate the server issuing instructions (with or without parameters) for the client to act, and it is trivial for side effects of those actions to be the creation of properties of arbitrary objects on the client, and even for those objects to be executable. But when the designer insists that the medium of communication should be truly arbitrary javascript source code then it is that design decision that introduces any implied necessity to evaluate code in the global execution context. Make a better design decision and the issue is irrelevant. |
#32
| |||
| |||
|
#33
| |||
| |||
|
|
[Re: dynamic script injection/insertion/obsession] Randy meet Andrea URL: http://webreflection.blogspot.com/20...valuation.html |

#34
| |||||||||||
| |||||||||||
|
|
If you append to the head element(or any other element) and then instantly remove that script then it is eligible for garbage collection and thus makes it a bad idea. JavaScript is injected inside browser parser (engine), DOM is another |
|
Especially if that script block has a function in it that you want to use later. It may still be there and it may not. That is why I started using the div element approach. This approach is obtrusive for every other script and for DOM too. |
|
Another problem with the code is in the evalScript function where it uses .text to set the text property of a script element. In a Windows based environment, that is almost foolproof. what's foolproof? text, using a script element, works perfectly with |
|
Run it in a Mac browser and your success ratio (across browsers) will drop to around 50% or so as there are at least 7 (probably more) that don't support setting the .text property on a script element and 6 (that I know of) that do support setting it. I say 50% simply because I am pretty sure the statistics there are skewed as I don't have a mac to investigate with. I have not a Mac too, just Safari for windows but I've a friend with |
|
The only browser, that I know of, that doesn't support createElement on a SCRIPT element is IE and that is what has led to a lot of this coding is an effort to cope with IE not implementing createElement on a script element. If it did, you would code it like this: var newScript = document.createElement('script'); newScript.type = "text/javascript"; newScriptText = "alert('createTextNode worked')"; var s = document.createTextNode(newScriptText); newScript.appendChild(s); It's a simple proposal enanchment, however I tested my proposal with |
|
And then you appendChild newScript to a container (whether it be HEAD, BODY or another container element). Then you merely have to figure out how to cope with IE. .... |
|
I am not sure it is as "strongly compatible" as you suggest the failure of setting the .text property in many mac browsers. please, just test them ... I usually develop for these browsers: |
|
As for your second blog entry, I disagree with these two statements: I can always say "eval is evil" in that regards because there *IS* an alternative/different approach to it and eval introduces problems that dynamic script insertion doesn't introduce. Predominantly it is a scope issue. So do You prefere low speed and error prone code (pure JS) parsing |
|
Second, I don't believe that code evaluation is "always" a bad practice. I said it's always a BAD practice, in rare case, it's a must. |
|
That said, the major difference in most of my code and research and what you posted is that I am looking and working on a basic framework approach to answer a very generic question and your post is in regards to a very specific question. Generic question: "I retrieved a document using AJAX, insert it in the page and my scripts don't get executed. How do I cause those script blocks to get executed?" Answer is on my blog, evalScript does it simply and quickly, 5 lines |
|
And with that question, the answer has to encompass script text, script source, scope issues, document.write issues, along with some other issues. Is your research based on document.write method? ... how many days did |
#35
| |||||||||||||||||||
| |||||||||||||||||||
|
|
If you append to the head element(or any other element) and then instantly remove that script then it is eligible for garbage collection and thus makes it a bad idea. JavaScript is injected inside browser parser (engine), DOM is another thing ... please show me only one example where a removed script causes what You're talking about. |
|
Especially if that script block has a function in it that you want to use later. It may still be there and it may not. That is why I started using the div element approach. This approach is obtrusive for every other script and for DOM too. |
|
Another problem with the code is in the evalScript function where it uses .text to set the text property of a script element. In a Windows based environment, that is almost foolproof. what's foolproof? |
|
text, using a script element, works perfectly with IE5, IE 5.5, IE 6 and IE7 |
|
Maybe You're thinking about createElement("style") and text IE problems? |
|
Run it in a Mac browser and your success ratio (across browsers) will drop to around 50% or so as there are at least 7 (probably more) that don't support setting the .text property on a script element and 6 (that I know of) that do support setting it. I say 50% simply because I am pretty sure the statistics there are skewed as I don't have a mac to investigate with. I have not a Mac too, just Safari for windows but I've a friend with Safari 2 and Mac, I'll ask him if my proposal will cause some problem. Until this, please tell me exactly what browser for what OSX causes problems, specifing platform and browser version, thank You. |
|
The only browser, that I know of, that doesn't support createElement on a SCRIPT element is IE and that is what has led to a lot of this coding is an effort to cope with IE not implementing createElement on a script element. If it did, you would code it like this: var newScript = document.createElement('script'); newScript.type = "text/javascript"; newScriptText = "alert('createTextNode worked')"; var s = document.createTextNode(newScriptText); newScript.appendChild(s); It's a simple proposal enanchment, however I tested my proposal with every used IE (5,5.5,6,7) and it seems to work perfectly. Maybe You're talking about Mac IE 5.X .... this death browser? |
|
And then you appendChild newScript to a container (whether it be HEAD, BODY or another container element). Then you merely have to figure out how to cope with IE. I am not sure it is as "strongly compatible" as you suggest the failure of setting the .text property in many mac browsers. please, just test them ... I usually develop for these browsers: IE6, IE7, FF1.5, FF2, FF3, Safari2, Safari3, Opera8, Opera9 |
|
in this case this solution works with IE5 and 5.5 too ... so, these are 99.9% of used browsers. |
|
Do You write code for IE4 too? In this case, good luck for your solution!!! |
|
As for your second blog entry, I disagree with these two statements: I can always say "eval is evil" in that regards because there *IS* an alternative/different approach to it and eval introduces problems that dynamic script insertion doesn't introduce. Predominantly it is a scope issue. So do You prefere low speed and error prone code (pure JS) parsing instead of core evaluation after a stable RegExp? So don't You think that a script tag inside a layout is just a sort of evaluation? I'm a bit confused about your position when You talk about *JavaScript* ... |
|
Second, I don't believe that code evaluation is "always" a bad practice. I said it's always a BAD practice, in rare case, it's a must. |
|
I suppose the only one obsessed by evaluation are people who don't think about language nature itsself, *SCRIPTING* ... so use eval if it's strongly necessary or spend much time to find slower alternatives (so abort D. Crockford toJSONString method and future FF implementation too, isn't right?) |
|
That said, the major difference in most of my code and research and what you posted is that I am looking and working on a basic framework approach to answer a very generic question and your post is in regards to a very specific question. Generic question: "I retrieved a document using AJAX, insert it in the page and my scripts don't get executed. How do I cause those script blocks to get executed?" Answer is on my blog, evalScript does it simply and quickly, 5 lines of JS code, aren't enough? |
|
And with that question, the answer has to encompass script text, script source, scope issues, document.write issues, along with some other issues. Is your research based on document.write method? |
|
how many days did You spend to perform this research? |
|
I think that's not so difficult to read a solution and use them, You spoke about IE problems while IE is perfectly compatible and You talked about document.write that's never a good practice on DOM loaded. |
|
I wonder, at this point, if You like jQuery solution that's totally wrong, |
|
asyncronous with safari and not useful to solve the problem with a call and window as first argument, while You (seems to) hate my solution that's compatible with safari too and works pretty right with 100% of (mine) tested browsers. |
#36
| |||||
| |||||
|
|
How do you debug code that doesn't exist anymore? There are more reasons to leave it than there are to remove it. It is a choice. I choose to leave mine. my debugger works fine and DOM is not persintently incremented |
|
Say again? How is me putting script blocks inside a sole div element obtrusive for every other script? just think about document.getElementsByTagName("script") ... and just |
|
The only possible problem that could *ever* be caused by it is if a div element happened to be duplicated in which case the script (mine) could be modified to create it's own div element to use. |
|
What is foolproof on a PC is to set the .text property of a script element. Trying to do that on other OS'es is a recipe for disaster. disaster ... LOL!!! |
|
But it does *not* work in 7 mac browsers (and that is only the ones I know about). as I've said, I don't have Mac and I don't care, usually, on browser |

#37
| |||
| |||
|
#38
| ||||
| ||||
|
|
Ok Randy, You have a credit inside my code update. |
|
Last line, that doesn't require above clear function and it should be cross-browser enough to forget this problem, could You confirm it?http://www.devpro.it/code/163.html |
|
Sorry for my self-packing obsession, however if someone is interested about source, just indent (beautyfing) them, it should be quite clear to read / understand :-) |
|
(evalScript = function(e) { var h = evalScript.node, s = document.createElement("script"); s.type = "text/javascript"; s.text = e; h.appendChild(s); h.removeChild(s); }).node = document.getElementsByTagName("head")[0] || document.getElementsByTagName("*")[0]; // self packed, (more) cross browser/platform alternative // Special thanks to Randy Webb [comp.lang.javascript] (function() { var head = document.getElementsByTagName("head")[0] || document.getElementsByTagName("*")[0]; evalScript = /webkit|khtml|shiira|sunrise/ i.test(navigator.userAgent) ? function(E) { var s = document.createElement('script'); s.type = "text/javascript"; s.appendChild(document.createTextNode(E)); head.appendChild(s); head.removeChild(s); } : ! /icab|ie 5\.2/i.test(navigator.userAgent) ? function(E) { var s = document.createElement('script'); s.type = 'text/javascript'; s.text = E; head.appendChild(s); head.removeChild(s) } : function(E) { var div = document.createElement("div"), style = div.style; style.position = "absolute"; style.border = style.height = style.left = style.top = style.width = "0px"; head.appendChild(div); evalScript = function(e) { div.innerHTML = '<script type"text/javascript">'+ E + '</script>'; divr.emoveChild(div.firstChild) }; evalScript(E) } })(); |
#39
| |||
| |||
|
|
On Aug 31, 4:07 am, Randy Webb <HikksNotAtH... (AT) aol (DOT) com> wrote: But it does *not* work in 7 mac browsers (and that is only the ones I know about). as I've said, I don't have Mac and I don't care, |
|
usually, on browser used by 0.01% of net surfers. |
|
However, thank You for your link, it seems that my basic proposal, that I've never called perfect but I called them *proposal*, need just a simple try catch or to be wrote differenlty for iCab and others using TextNode, really simple, isn't it? Well done, I'll update my code ... so, at this point, totally amount of 2 hours against 7 years? :P |
#40
| ||||||
| ||||||
|
|
I'm curious if he wants credit in your code. Since He spent much more time than me to try to solve this problem, I |

|
"cross-browser enough"? Script insertion is only multi-browser. probably my bad english doesn't allow me to explain perfectly my |
|
In addition to the brittle dependence on navigator.userAgent, I see at least two superficial bugs. I found one, bad replacement (function(e) instead of E) ... now, |
|
as I've said, I don't have Mac and I don't care, That is an unexpected attitude for a professional. come on guys, I wrote JSL many times ago ... so please don't teach me |
|
usually, on browser used by 0.01% of net surfers. This is definitely way off. .... |
|
But where did your two hours get you? two hours to: write code, do few tests with "every day" browsers, |

![]() |
| Thread Tools | |
| Display Modes | |
| |