Double-document.write('<scr'+'ipt>...) insert <br>-like space?

Discussion in 'Javascript' started by Richard, Aug 24, 2007.

  1. Richard

    Richard Guest

    <div id="right" >
    <script type="text/javascript">
    if (IsThis)
    document.write('<img src="images/foo.gif" />');
    else
    {
    document.write('<div id="too_right"><scr' + 'ipt type="text/
    javascript" src="http://external.com/script.js"></scr' + 'ipt></
    div>');
    }
    </script>
    </div>

    Now here's the good part: http://external.com/script.js returns (wait
    for it) 'document.write("<img src="..." />");'

    This actually works, except that it acts like I have put in <br/>
    elements. The <img> is top-aligned (good) but the image returned by
    the script.js is pushed to the bottom, AND (worse) the whole <div>
    height is increased, pushing all the later elements on the page down.

    Does the legacy of write() imply a newline at the end?

    If CSS is the answer, I would prefer not trying to position the
    "right" div; trying to restrain the height would be easier.
     
    Richard, Aug 24, 2007
    #1
    1. Advertising

  2. Richard

    RobG Guest

    Randy Webb wrote:
    [...]
    > Then why not just write the image tag anyway? Or, is the src attribute
    > different?
    >
    > if (!IsThis){document.write('<div id="too_right">')}
    > document.write('<img src="images/foo.gif">');
    > if (!IsThis){document.write('</div>')}


    I am always wary of writing opening and closing tags in separate
    document.write statements. It is much better I think to build the
    string, then write it in one go:

    var html = '';
    if (IsThis) {
    html += '<img src="images/foo.gif">';
    } else {
    html += '<div id="too_right"><script'
    + ' type="text/javascript" '
    + ' src="http://external.com/script.js">'
    + '<\/script><\/div>';
    }
    document.write(html);


    --
    Rob
    "We shall not cease from exploration, and the end of all our
    exploring will be to arrive where we started and know the
    place for the first time." -- T. S. Eliot
     
    RobG, Aug 25, 2007
    #2
    1. Advertising

  3. Richard

    Richard Guest

    1. Do you mean the 'scr' + 'ipt' thing? I had some browser get
    confused and end the real script tag once. Perhaps it is unnecessary
    these days.

    2. The goal is to sometimes embed the <img> and sometimes embed the
    <script>.

    3. I have to do it this way because I do not own script.js; I cannot
    control what it does, and currently, it does a document.write(). If I
    want to use it based on a JavaScript value, I have to do this nested
    document.write weirdness processing at page parse-time. I would prefer
    to use div.innerHTML = ..., but as the external script returns
    "document.write(...)", that is not an option.

    4. Why XHTML? XML editors prefer it, and they will tell me if I am
    missing something. While 90% of the web might not know what it is,
    they do not seem to mind it.

    Cheers

    On Aug 24, 5:23 pm, Randy Webb <> wrote:
    > Richard said the following on 8/24/2007 6:10 PM:
    >
    > > <div id="right" >
    > > <script type="text/javascript">
    > > if (IsThis)
    > > document.write('<img src="images/foo.gif" />');
    > > else
    > > {
    > > document.write('<div id="too_right"><scr' + 'ipt type="text/
    > > javascript" src="http://external.com/script.js"></scr' + 'ipt></
    > > div>');

    >
    > What is with all that voodoo scripting by breaking up tags that have no
    > bearing on what it was attempting to do rather than breaking up the
    > sequence that should be broken up?
    >
    > document.write('<script type="text/javascript"
    > src="someFile.js"><\/script>')
    >
    > > }
    > > </script>
    > > </div>

    >
    > > Now here's the good part:http://external.com/script.jsreturns (wait
    > > for it) 'document.write("<img src="..." />");'

    >
    > Then why not just write the image tag anyway? Or, is the src attribute
    > different?
    >
    > if (!IsThis){document.write('<div id="too_right">')}
    > document.write('<img src="images/foo.gif">');
    > if (!IsThis){document.write('</div>')}
    >
    > And for crying out loud, stop using XHTML on the web. Especially when
    > 90% or so of the web doesn't even know what it is.
    >
    > --
    > Randy
    > Chance Favors The Prepared Mind
    > comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
    > Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/
     
    Richard, Aug 25, 2007
    #3
  4. Richard

    RobG Guest

    On Aug 25, 11:16 am, Richard <> wrote:
    > 1. Do you mean the 'scr' + 'ipt' thing? I had some browser get
    > confused and end the real script tag once. Perhaps it is unnecessary
    > these days.


    Please do not top-post, reply below trimmed quotes.

    The characters </ are the official official end-of-tag markers, so
    that's want you need to "hide" from the browser (although most will
    not end it until </script> is encountered).


    [...]
    > 4. Why XHTML? XML editors prefer it, and they will tell me if I am
    > missing something.


    Your choice of editor should not drive the language you use, it should
    be the other way around.


    > While 90% of the web might not know what it is,
    > they do not seem to mind it.


    <URL: http://www.spartanicus.utvinternet.ie/no-xhtml.htm >


    --
    Rob
     
    RobG, Aug 25, 2007
    #4
  5. RobG wrote:
    > On Aug 25, 11:16 am, Richard <> wrote:
    >> 1. Do you mean the 'scr' + 'ipt' thing? I had some browser get
    >> confused and end the real script tag once. Perhaps it is unnecessary
    >> these days.

    >
    > [...]
    > The characters </ are the official official end-of-tag markers, so
    > that's want you need to "hide" from the browser (although most will
    > not end it until </script> is encountered).


    Note that this condition applies to CDATA (character data) content only (and
    IIRC not to XML applications). In XHTML the content model of the `script'
    element is PCDATA (parsed character data), where no such restriction exists
    (there exist other restrictions such as escaping markup characters, or
    including the `script' content in a CDATA declaration block, or you don't
    use inline scripts at all).

    However, if the XHTML is HTML-compatible and when served as text/html, the
    condition mentioned by you would apply as well, because so far there is no
    context-sensitive parser selection implemented in UAs (it all depends on
    the Content-Type; the W3C Validator in contrast is context-sensitive).

    > [...]
    >> 4. Why XHTML? XML editors prefer it, and they will tell me if I am
    >> missing something.

    >
    > Your choice of editor should not drive the language you use, it should
    > be the other way around.


    But these *two* are generally considered valid arguments for using XHTML.
    However, he should then do it properly, i.e. write HTML-compatible XHTML
    as much as possible (it can't be done with `<br />' as `<br></br>' for
    example), and serve it as application/xhtml+xml to UAs which support that
    (Accept-* evaluation, either through built-in or user-defined Content
    Negotation).


    PointedEars
    --
    "Use any version of Microsoft Frontpage to create your site. (This won't
    prevent people from viewing your source, but no one will want to steal it.)"
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>
     
    Thomas 'PointedEars' Lahn, Aug 25, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,566
    Victor Bazarov
    Feb 17, 2005
  2. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,937
    Bryan Bullard
    Jul 11, 2003
  3. Christian Seberino
    Replies:
    21
    Views:
    1,719
    Stephen Horne
    Oct 27, 2003
  4. Ian Bicking
    Replies:
    2
    Views:
    1,056
    Steve Lamb
    Oct 23, 2003
  5. Richard
    Replies:
    0
    Views:
    118
    Richard
    Aug 24, 2007
Loading...

Share This Page