473,372 Members | 844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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.value;
var body = "<BODY style='background:" + img + "'>";
iView.focus();
iView.document.write(body);
}

But I get this HTML when the page is saved:

<BODY style="BACKGROUND: 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.
Jul 23 '05 #1
2 2482
William Starr Moake wrote on 13 jun 2004 in comp.lang.javascript:
function backImg() {
var img = form2.imgurl.value;
var body = "<BODY style='background:" + img + "'>";
iView.focus();
iView.document.write(body);
}


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

not tested.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #2
William Starr Moake wrote:
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.value;
var body = "<BODY style='background:" + img + "'>";
iView.focus();
iView.document.write(body);
}

But I get this HTML when the page is saved:

<BODY style="BACKGROUND: 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?


Try

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

instead because

<BODY style='background: 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.backgroundImage =
"url(" + encodeURI(img) + ")";

which *preserves* the current content *and* changes the background image.
PointedEars
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: bhennon | last post by:
Hey all, I have a small php script that calls a random image at the following page. http://www.2006ymcanationals.com/random.php IT WORKS IF I go directly to the above link. I am trying to...
14
by: Akbar | last post by:
Hey there, Big-time curiosity issue here... Here's the test code (it's not that long)... it's to display a large number of image links with captions, ideally pulled in from an external file...
2
by: Dennis | last post by:
This may be easy for most but I can't get this thing to work. I believe I followed all the instructions but when I click on the link no window opens just the default IE page cannot display. Here is...
9
by: Flynn | last post by:
My understanding of javascript is very limited. Could some one help me out. I found this code for an image gallery viewer. It works fine on my site, but I'd like to modify it so that the mainpic...
1
by: Allen | last post by:
I am trying to add an additional photo/hyperlink to the company web site (I didn't create it) without any luck. The mouseover feature 'highlights' pics by swapping them with another pic using this...
1
by: Russ | last post by:
I've been trying to get my head around this for 3 days now and it seems like everything I try does not work for one reason or another. I built a test page using the TabStrip and MultiPage controls....
4
by: Tony | last post by:
I have a function that loops through a set of images but at the end of the loop I need the page to go to a new location. Does anyone know how force my page to go there. This is what I am...
3
tolkienarda
by: tolkienarda | last post by:
hi all i have a problem i have a script that i got working exactly the way i wanted it. and then i tried to intrigate it into the site and it totaly stopped working. below is the original code ...
1
by: kksandeep | last post by:
i am using this three files to uplod file. i got this file from net but i think these have some error. i am new to this field plz help the script i found is some helpful but not too that i need ...
6
by: sarika | last post by:
Hi All i m making a web site having number of big images. My requirement is i want to dunamically split the images while downloading so that my website works fast and image downloading does not...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.