script to set page bg image

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Starr Moake

    #1

    script to set page bg image

    For an IE-based offline WYSIWYG editor I'm developing, I have added
    javascript to insert images as table backgrounds and to set page
    background color. But I can't get any script to work for setting a
    page background image.

    I use a form with a button input type="file" for the user to select a
    page background image from his hard drive and a second button with
    onClick to launch this script:

    function backImg() {
    var img = form2.imgurl.va lue;
    var body = "<BODY style='backgrou nd:" + img + "'>";
    iView.focus();
    iView.document. write(body);
    }

    But I get this HTML when the page is saved:

    <BODY style="BACKGROU ND: none"></BODY>

    The MSHTML control is ignoring the var img input and it doesn't allow
    access to the <head> section for CSS input. I've tried dozens of
    variations of the script, but none work. Any ideas?

    PS: I have to use BODY style= rather than body background= because
    it's proprietary in MSHTML.
  • Evertjan.

    #2
    Re: script to set page bg image

    William Starr Moake wrote on 13 jun 2004 in comp.lang.javas cript:
    [color=blue]
    > function backImg() {
    > var img = form2.imgurl.va lue;
    > var body = "<BODY style='backgrou nd:" + img + "'>";
    > iView.focus();
    > iView.document. write(body);
    >}
    >[/color]

    function backImg() {
    var img = "url("+document .forms['form2'].imgurl.value+" )";
    iView.document. body.style.back groundImage=img ;
    }

    not tested.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: script to set page bg image

      William Starr Moake wrote:[color=blue]
      > For an IE-based offline WYSIWYG editor I'm developing, I have added
      > javascript to insert images as table backgrounds and to set page
      > background color. But I can't get any script to work for setting a
      > page background image.
      > [...]
      > function backImg() {
      > var img = form2.imgurl.va lue;
      > var body = "<BODY style='backgrou nd:" + img + "'>";
      > iView.focus();
      > iView.document. write(body);
      > }
      >
      > But I get this HTML when the page is saved:
      >
      > <BODY style="BACKGROU ND: none"></BODY>
      >
      > The MSHTML control is ignoring the var img input and it doesn't allow
      > access to the <head> section for CSS input. I've tried dozens of
      > variations of the script, but none work. Any ideas?[/color]

      Try

      var body =
      "<BODY style='backgrou nd-image:url(" + encodeURI(img) + ")'>";

      instead because

      <BODY style='backgrou nd: foobar.png'>

      is clearly invalid CSS, since 'foobar.png' is not a valid property
      value. So it is highly likely that IE uses the default value "none"
      instead.

      Note that document.write( ) usually *overwrites* the document after
      loading has finished, so that may not be the best approach. Provided
      that the object referred to with "iView" provides an (incomplete)
      implementation of W3C DOM Level 2, you could use

      iView.document. body.style.back groundImage =
      "url(" + encodeURI(img) + ")";

      which *preserves* the current content *and* changes the background image.


      PointedEars

      Comment

      Working...