|
|
|
Next: Can I retrieve the object of the window opened by..
|
| Author |
Message |
External

Since: Feb 16, 2004 Posts: 2574
|
(Msg. 16) Posted: Thu Aug 09, 2007 2:09 am
Post subject: Re: Dynamic Script Insertion Revisited Archived from groups: comp>lang>javascript (more info?)
|
|
|
Randy Webb wrote:
> Richard Cornford said the following on 8/8/2007 7:01 PM:
>> Randy Webb wrote:
>> <snip>
>>> The problem was, and still is, that IE - no version -
>>> will allow you to use createTextNode and then
>>> appendChild it to a Script element.
>> <snip>
>>
>> Internet Explorer Elements have a boolean - canHaveChildren -
>> property.
>
> Is IE the only one that implements canHaveChildren?
It is the only one I have noticed, but some manufacturers strive to
imitate IE and if they notice that the property exists they may well
reproduce it, but whilst doing so may not restrict the ability of the
script element to have children.
>> For script elements it has a false value. It doesn't represent
>> the whole solution but I would think that if you found that a
>> script element had a - canHaveChildren - property of boolean
>> type, and that it was false, then attempting to add child nodes
>> of any sort would look line a non-viable activity.
>
> if ((typeof(newScript.canHaveChildren) === 'boolean') &&
> (newScript.canHaveChildren === false))
Having verified that the property is of boolean type I would not bother
with the comparison in the second test. Just NOT - canHaveChildren -
would do.
> I don't think a test to see if it is false or not would be
> required but probably wouldn't hurt to include
I would say that the second test is necessary to complete the logic of
the test.
> it just in case some other browser is out or comes out that implements
> canHaveChildren.
And that as well.
> I have it noted in a Notes file I have on script insertion
> so that I can go back to it and give it some more thought later.
<snip>
If I had time to look at this properly I might look at putting a script
element on a page that had code contents and then examining it to see
what type of children it did have (if any). If Text node(s) then that
may be good grounds for particular actions. Though logically you could
suspect that some would (or may) have CDATASection nodes (nodeType ==
4), given that in HTML the contents of a scirpt are interpreted as
CDATA.
Richard. |
|
| Back to top |
|
 |  |
External

Since: Aug 06, 2007 Posts: 358
|
(Msg. 17) Posted: Thu Aug 09, 2007 2:30 am
Post subject: Re: Dynamic Script Insertion Revisited Archived from groups: per prev. post (more info?)
|
|
|
On Aug 9, 2:21 am, David Mark <dmark.cins....RemoveThis@gmail.com> wrote:
> On Aug 8, 10:28 pm, Randy Webb <HikksNotAtH....RemoveThis@aol.com> wrote:
>
> > David Mark said the following on 8/8/2007 9:32 PM:
>
> > > On Aug 8, 8:27 pm, Randy Webb <HikksNotAtH....RemoveThis@aol.com> wrote:
>
> [snip]
>
>
>
> > Thanks for the input and insight.
>
> No problem. I was sufficiently intrigued by this problem that I gave
> it another go with the third branch included.
Oops. I just realized that my window.onerror afterthought has a
flaw. It should define the executeJSString the same as if all three
tests fail (and put the old error handler back.) And of course, if
one of the three fail with an error (something I tried to avoid with
feature detection throughout), the other tests won't be tried.
The only real value of the window.onerror handler in this example is
to prevent a script error from being reported to the user. The
alternative of using try/catch clauses for the tests you think might
cause an error (and putting the whole thing in its own file) is
looking like a better idea. |
|
| Back to top |
|
 |  |
External

Since: Aug 06, 2007 Posts: 358
|
(Msg. 18) Posted: Thu Aug 09, 2007 4:57 pm
Post subject: Re: Dynamic Script Insertion Revisited Archived from groups: per prev. post (more info?)
|
|
|
On Aug 9, 4:40 pm, "Richard Cornford" <Rich... RemoveThis @litotes.demon.co.uk>
wrote:
> David Mark wrote:
> > On Aug 8, 8:27 pm, Randy Webb wrote:
> >> Richard Cornford said the following on 8/8/2007 7:01 PM:
> >>> Randy Webb wrote:
> >>> <snip>
> >>>> The problem was, and still is, that IE - no version - will
> >>>> allow you to use createTextNode and then appendChild it to
> >>>> a Script element.
> >>> <snip>
>
> >>> Internet Explorer Elements have a boolean - canHaveChildren -
> >>> property.
>
> >> Is IE the only one that implements canHaveChildren?
>
> > I believe it is IE5.5 and up.
>
> Your belief is false. Windows IE 5 certainly had this property. IE 4 may
> also have had the property, as it was the browser that had 'children'
> collections and the name of the property is not 'canHaveChildNodes'.
Who said it was "canHaveChildNodes?"
>
> > Other may have copied it, but it seems
> > doubtful.
>
> It would not matter if they did, so long as they did not implement them
> irrationally and attach true values to elements that would reject
> attempts to add children.
Right.
>
> <snip>
>
> > This seems like the solution:
>
> > function executeJSString(stringToExecute) {
> > var newScript = document.createElement('script');
> > newScript.type = "text/javascript";
>
> > if (document.createTextNode && (
> > typeof(newScript.canHaveChildren) == 'undefined' ||
> > (typeof(newScript.canHaveChildren) == 'Boolean' &&
> > newScript.canHaveChildren))) {
>
> I don't see much point in testing the type of the property more than
> once. It only seems worth checking that it is boolean and then branching
> on its trueness if it is.
If it is undefined (all but IE) then you want to try createTextNode as
it often works. If it is boolean and true, then you also want to try
it, though this scenario never seems to come up and probably won't
until IE adds the ability to add children to a script element.
>
> Though I would expect to see a test for an - appendChild - (and
> possibly - insertBefore - ) method on the element, as the absence of
> either might also be taken a good grounds for expecting the failure of
> the attempt to add a new child.
I didn't add all of the feature testing needed to this snippet. I did
address all of the issues you mention in the next post. I think the
"full" solution covers everything, but I admittedly botched the
window.onerror handling.
I may post one more revision to this as I did come up with a way to
make the window.onerror trick work. I'll wait to see if try/catch is
deemed viable for the solution. |
|
| Back to top |
|
 |  |
External

Since: Feb 16, 2004 Posts: 2574
|
(Msg. 19) Posted: Thu Aug 09, 2007 9:40 pm
Post subject: Re: Dynamic Script Insertion Revisited Archived from groups: per prev. post (more info?)
|
|
|
David Mark wrote:
> On Aug 8, 8:27 pm, Randy Webb wrote:
>> Richard Cornford said the following on 8/8/2007 7:01 PM:
>>> Randy Webb wrote:
>>> <snip>
>>>> The problem was, and still is, that IE - no version - will
>>>> allow you to use createTextNode and then appendChild it to
>>>> a Script element.
>>> <snip>
>>
>>> Internet Explorer Elements have a boolean - canHaveChildren -
>>> property.
>>
>> Is IE the only one that implements canHaveChildren?
>
> I believe it is IE5.5 and up.
Your belief is false. Windows IE 5 certainly had this property. IE 4 may
also have had the property, as it was the browser that had 'children'
collections and the name of the property is not 'canHaveChildNodes'.
> Other may have copied it, but it seems
> doubtful.
It would not matter if they did, so long as they did not implement them
irrationally and attach true values to elements that would reject
attempts to add children.
<snip>
> This seems like the solution:
>
> function executeJSString(stringToExecute) {
> var newScript = document.createElement('script');
> newScript.type = "text/javascript";
>
> if (document.createTextNode && (
> typeof(newScript.canHaveChildren) == 'undefined' ||
> (typeof(newScript.canHaveChildren) == 'Boolean' &&
> newScript.canHaveChildren))) {
I don't see much point in testing the type of the property more than
once. It only seems worth checking that it is boolean and then branching
on its trueness if it is.
Though I would expect to see a test for an - appendChild - (and
possibly - insertBefore - ) method on the element, as the absence of
either might also be taken a good grounds for expecting the failure of
the attempt to add a new child.
Richard. |
|
| Back to top |
|
 |  |
External

Since: Aug 24, 2004 Posts: 4981
|
(Msg. 20) Posted: Sat Aug 11, 2007 10:53 am
Post subject: Re: Dynamic Script Insertion Revisited Archived from groups: per prev. post (more info?)
|
|
|
David Mark said the following on 8/9/2007 2:21 AM:
> On Aug 8, 10:28 pm, Randy Webb <HikksNotAtH... RemoveThis @aol.com> wrote:
>> David Mark said the following on 8/8/2007 9:32 PM:
>>> On Aug 8, 8:27 pm, Randy Webb <HikksNotAtH... RemoveThis @aol.com> wrote:
> [snip]
>> Thanks for the input and insight.
>>
>
> No problem. I was sufficiently intrigued by this problem that I gave
> it another go with the third branch included.
Welcome to HikkScript, I have been intrigued with dynamic JS insertion
for a very long time  If it will give you some idea of how long, I can
load JS dynamically in Netscape 4.
> I think this should cover "everything" without resorting to try/
> catch. I tested with the usual suspects. Other than the single dummy
> variable, it is unobtrusive and results in a function that returns a
> success flag.
>
> BTW, I tried short-circuiting all but the innerHTML method and found
> that it didn't work on Windows browsers at all (inserted script
> doesn't execute.) I'm surprised it works on Mac versions.
I discovered it in very early versions of Netscape 6 in testing why
people couldn't get it to work in IE when it "works in Netscape". The
finding of it working in Mac browsers was a by product of me knowing it
worked on NS6/Win and giving it a whirl in Mac browsers by having the
innerHTML button on the test/date page.
I have been out of town the last 2 days and will give this a thorough
testing/lookover this weekend. The major thing this thread has found me
is the canHaveChildren property.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/ |
|
| Back to top |
|
 |  |
External

Since: Aug 24, 2004 Posts: 4981
|
(Msg. 21) Posted: Sat Aug 11, 2007 10:56 am
Post subject: Re: Dynamic Script Insertion Revisited Archived from groups: per prev. post (more info?)
|
|
|
David Mark said the following on 8/9/2007 7:57 PM:
> On Aug 9, 4:40 pm, "Richard Cornford" <Rich... RemoveThis @litotes.demon.co.uk>
> wrote:
>> David Mark wrote:
>>> On Aug 8, 8:27 pm, Randy Webb wrote:
>>>> Richard Cornford said the following on 8/8/2007 7:01 PM:
>>>>> Randy Webb wrote:
>>>>> <snip>
>>>>>> The problem was, and still is, that IE - no version - will
>>>>>> allow you to use createTextNode and then appendChild it to
>>>>>> a Script element.
>>>>> <snip>
>>>>> Internet Explorer Elements have a boolean - canHaveChildren -
>>>>> property.
>>>> Is IE the only one that implements canHaveChildren?
>>> I believe it is IE5.5 and up.
>> Your belief is false. Windows IE 5 certainly had this property. IE 4 may
>> also have had the property, as it was the browser that had 'children'
>> collections and the name of the property is not 'canHaveChildNodes'.
>
> Who said it was "canHaveChildNodes?"
It is an inference. IE5 had childNodes. If IE4 had childNodes and not
the children collection, then the property would probably have been
named canHaveChildNodes, since it is named canHaveChildren then it
reasons that IE4 had the canHavechildren property and that is how it got
named.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/ |
|
| Back to top |
|
 |  |
External

Since: Jul 05, 2007 Posts: 41
|
(Msg. 22) Posted: Fri Aug 31, 2007 3:03 am
Post subject: Re: Dynamic Script Insertion Revisited Archived from groups: per prev. post (more info?)
|
|
|
On Aug 8, 9:56 am, Randy Webb <HikksNotAtH... RemoveThis @aol.com> wrote:
> In this thread:
>
> <URL:http://groups.google.com/group/comp.lang.javascript/browse_frm/thread...>
>
> The problem was, and still is, that IE - no version - will allow you to
> use createTextNode and then appendChild it to aScriptelement. It
> throws a "Unexpected call to method or property access" error.
>
> The only browsers, that I am aware of, that don't support createTextNode
> on aSCRIPTelement are IE/Win, IE/MAC, and iCAB3.0.3 (Is there a newer
> iCAB?).
>
> iCab and IE/mac support neither .text or .createTextNode so for this
> code they have become part of the "Give up Randy, it won't work there"
> group.
>
> The browsers, again that I know of, that don't support the .text
> property are:
>
> IE/Mac
> iCab3.0.3
> Safari 1.3.2
> Safari 2.0.4
> Shiira 1.2.2
> Sunrise 0.89
>
> Safari surprised me on windows though as it supports .text, go figure.
>
> Where it left me was wanting to use .createTextNode for support and then
> falling back on the .text property.
>
> What I ended up with is this:
>
> var isIE = false;
> /*@cc_on @*/
> /*@if (@_jscript_version >= 4)
> var isIE = true;
> @end @*/
>
> function executeJSString(stringToExecute){
> var newScript = document.createElement('script');
> newScript.type = "text/javascript";
> if (isIE)
> {
> newScript.text = stringToExecute;
> }
> else
> {
> var s = document.createTextNode(stringToExecute);
> newScript.appendChild(s);
> }
> document.getElementById("scriptDiv").appendChild(newScript);
>
> }
>
> Where "scriptDiv" is a container forscriptelements. The actual code
> has code in it to empty the container so that I don't end up with
> duplicatescriptblocks and/or excessivescriptelements.
>
> The problem comes in if a browser, other than IE, supports .text and not
> .createTextNode (or errors on it like IE does). That lead me to try to
> set the .text property of ascriptelement (on page load) to set a
> variable and then branch on that variable. The problem there comes in
> when the browser throws an error trying to set the .text property.
>
> function insertScript(scriptContents) {
> var useIt = false;
> var newScript = document.createElement('script');
> newScript.type = "text/javascript";
> newScript.text = "var useText=true";
> document.getElementById("myDiv").appendChild(newScript);
> //end text insert section
>
> var newScript = document.createElement('script');
> newScript.type = "text/javascript";
> if(useText){
> newScript.text = scriptContents;}
>
> else{
> var s = document.createTextNode(scriptContents);
> newScript.appendChild(s);}
>
> document.getElementById("scriptDiv").appendChild(newScript);
>
> }
>
> I don't have a browser that doesn't support .text so I can't test it
> thoroughly.
>
> If anybody has any other ideas on a true feature detection approach
> and/or that can test the above code on any non-windows browser I would
> be gratefully appreciative.
>
> Also, anybody that can test this page with a browser not listed (Other
> than Netscape 4.xx series and lower) and give me the results of the 5
> button clicks, I would again be gratefully appreciative.
>
> <URL:http://members.aol.com/_ht_a/hikksnotathome/loadJSFile/>
>
Randy,
Can you include tests for scripts that have defer attribute?
That will have different results for innerHTML
_________________+ safari2/Mac | webkit/Mac(419.3)
change src | n | n |
change innerHTML | n | n |
createElement | Y | n |
change .text | n | Y |
createTextNode | Y | Y |
The chart will be more complete with testing the defer attribute on
innerHTML injections.
Garrett
> --
> Randy
> Chance Favors The Prepared Mind
> comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
> Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/ |
|
| Back to top |
|
 |  |
External

Since: Aug 24, 2004 Posts: 4981
|
(Msg. 23) Posted: Fri Aug 31, 2007 3:03 am
Post subject: Re: Dynamic Script Insertion Revisited Archived from groups: per prev. post (more info?)
|
|
|
dhtmlkitchen DeleteThis @gmail.com said the following on 8/30/2007 11:47 PM:
> On Aug 8, 9:56 am, Randy Webb <HikksNotAtH... DeleteThis @aol.com> wrote:
<snip>
>> <URL:http://members.aol.com/_ht_a/hikksnotathome/loadJSFile/>
>>
> Randy,
>
> Can you include tests for scripts that have defer attribute?
Are you saying that you get different results for Safari with and
without a defer attribute? The page, as written, has a defer attribute
in the script that it tries to insert via innerHTML.
> That will have different results for innerHTML
>
> _________________+ safari2/Mac | webkit/Mac(419.3)
> change src | n | n |
> change innerHTML | n | n |
> createElement | Y | n |
> change .text | n | Y |
> createTextNode | Y | Y |
>
>
> The chart will be more complete with testing the defer attribute on
> innerHTML injections.
I will add one in the next day or so that doesn't have a defer attribute
and see if any testing shows different results for them.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet? |
|
| Back to top |
|
 |  |
External

Since: Jul 05, 2007 Posts: 41
|
(Msg. 24) Posted: Fri Aug 31, 2007 8:27 pm
Post subject: Re: Dynamic Script Insertion Revisited Archived from groups: per prev. post (more info?)
|
|
|
On Aug 30, 10:12 pm, Randy Webb <HikksNotAtH....DeleteThis@aol.com> wrote:
> dhtmlkitc....DeleteThis@gmail.com said the following on 8/30/2007 11:47 PM:
>
> > On Aug 8, 9:56 am, Randy Webb <HikksNotAtH....DeleteThis@aol.com> wrote:
>
> <snip>
>
> >> <URL:http://members.aol.com/_ht_a/hikksnotathome/loadJSFile/>
>
> > Randy,
>
> > Can you include tests for scripts that have defer attribute?
>
> Are you saying that you get different results for Safari with and
> without a defer attribute? The page, as written, has a defer attribute
> in the script that it tries to insert via innerHTML.
>
no, I didn't actually look at your source code to see the defer
attribute.
> > That will have different results for innerHTML
>
> > _________________+ safari2/Mac | webkit/Mac(419.3)
> > change src | n | n |
> > change innerHTML | n | n |
> > createElement | Y | n |
> > change .text | n | Y |
> > createTextNode | Y | Y |
>
The column on the left represents a button in your test.
The other columns represent browsers. You can see that changing
".text" deosn't work in Safari 2, but does work in Webkit.
Webkit nightlies has lots of improvements over safari. Lots.
Garrett
> > The chart will be more complete with testing the defer attribute on
> > innerHTML injections.
>
> I will add one in the next day or so that doesn't have a defer attribute
> and see if any testing shows different results for them.
>
> --
> Randy
> Chance Favors The Prepared Mind
> comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
> Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/
> Answer:It destroys the order of the conversation
> Question: Why?
> Answer: Top-Posting.
> Question: Whats the most annoying thing on Usenet? |
|
| Back to top |
|
 |  |
External

Since: Aug 24, 2004 Posts: 4981
|
(Msg. 25) Posted: Fri Aug 31, 2007 8:27 pm
Post subject: Re: Dynamic Script Insertion Revisited Archived from groups: per prev. post (more info?)
|
|
|
dhtmlkitchen.DeleteThis@gmail.com said the following on 8/31/2007 4:27 PM:
> On Aug 30, 10:12 pm, Randy Webb <HikksNotAtH....DeleteThis@aol.com> wrote:
>> dhtmlkitc....DeleteThis@gmail.com said the following on 8/30/2007 11:47 PM:
>>
>>> On Aug 8, 9:56 am, Randy Webb <HikksNotAtH....DeleteThis@aol.com> wrote:
>> <snip>
>>
>>>> <URL:http://members.aol.com/_ht_a/hikksnotathome/loadJSFile/>
>>> Randy,
>>> Can you include tests for scripts that have defer attribute?
>> Are you saying that you get different results for Safari with and
>> without a defer attribute? The page, as written, has a defer attribute
>> in the script that it tries to insert via innerHTML.
>>
> no, I didn't actually look at your source code to see the defer
> attribute.
I don't think it will ever matter but I am adding another button so that
there are two innerHTML buttons - one with and one without a defer
attribute. I am going to have to work on the layout of the page for
alignment so it may be a few days or so before I get an updated version
uploaded.
>>> That will have different results for innerHTML
>>> _________________+ safari2/Mac | webkit/Mac(419.3)
>>> change src | n | n |
>>> change innerHTML | n | n |
>>> createElement | Y | n |
>>> change .text | n | Y |
>>> createTextNode | Y | Y |
>
> The column on the left represents a button in your test.
>
> The other columns represent browsers. You can see that changing
> ".text" deosn't work in Safari 2, but does work in Webkit.
>
> Webkit nightlies has lots of improvements over safari. Lots.
Is Webkit a browser or an engine? Sounds like an ignorant question but I
honestly don't know what it is. Right now, I have it listed locally like
this:
WebKit 419.3 OS X 10.3.9
Is your OS different? Or, should it be listed differently?
Also, do you have any other browser/OS combinations that are not listed
on that page?
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/ |
|
| Back to top |
|
 |  |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|
|