How to link CSS(s) already linked to parent frame into child iframeusing javascript

R

Richard Cornford

In retrospect I was thinking that the order here probably was not a
good idea. That is, it probably would be better to assign the - type -
prior to appending the element to the DOM.
`typeof ... == "object"` is a somewhat unsafe existence test.

That would depend on what you know at the point of making the test. It
isn't a great test here but we know that it is not going to be
executed unless the environment in question either is a known IE 5+ or
is trying pretty hard to imitate one.
In Internet Explorer (at least 8), there's "Enable DOM Storage"
option. When that option is unchecked, `window.localStorage`
evaluates to `null`. Obviously, `typeof window.localStorage`
then evaluates to "object" (and `"localStorage" in window`
evaluates to `true`, although this is unrelated to example
at hand).

It seems unlikely that IE is to ever introduce such possibility,
but if they do (or for whatever other reason), there's a chance
of `s2.styleSheet` being `null` and
`typeof s2.styleSheet == 'object'` evaluating to `true` in your
example. In that case, following line would result in an error.

Yes it would, unless in the meanwhile Microsoft change IE such that
the STYLE elements accept the setting of their content via the
insertion of a Text Node, in which case the value of - canHaveChildren
- changes to true and the browser never uses that second branch. That
seems quite a plausible change as people really do seem to like the
idea of creating SCRIPT and STYLE elements using DOM creation methods
(though I always note that it is TextNodes that get appended rather
than CDATASection Nodes getting any consideration (maybe a reflection
of the fact that there is not nearly as much XHTML in existence as the
impression that may be gained from looking at mark-up)).
Alternatives that come to mind:

// direct test for `null` value
if (typeof s2.styleSheet == "object" && s2.styleSheet !== null)

// boolean type conversion with typeof check before, to avoid type
converting "unknown" objects
if (typeof s2.styleSheet == "object" && s2.styleSheet)

// or just boolean type conversion (probably most unsafe one)
if (s2.styleSheet)
<snip>

Yes, there are alternatives, all more complex. In a general sense I
don't like the need to repeat this test when the thing that is being
tested is not going to change while the document is loaded. Re-
designing the code so that it only tested once and branched to, say,
assign one of two STYLE Element content assigning functions for later
use would make more elaborate testing more appealing.

But there is minimal worth in devoting time to this code. It is, after
all (and as has been pointed out), solving the problem in the worst
way possible (or solving the wrong problem).

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,575
Members
47,207
Latest member
HelenaCani

Latest Threads

Top