473,385 Members | 1,796 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,385 software developers and data experts.

Background changing depending on title.

I'm trying to write a code that will use css to change the background of a page dependent on the page title. The image used for the background has the same title as the page. I was trying to use the document.write code to write a css tag that sayed
Expand|Select|Wrap|Line Numbers
  1. <style>#content{background-image:url(images/{imagenamehere}.jpg);</style>
Can anyone help me with this?
Mar 20 '08 #1
5 1174
The javascript I have so far is as follows.
Expand|Select|Wrap|Line Numbers
  1. function background(){
  2. var name = document.title;
  3. var code = "<style>#content{background-image:url(/images/" + name + ".jpg);background-repeat:repeat-y;background-position:right;background-attachment:fixed;</style>";
  4. document.write(code);}
  5.  
This is inside a script tag in the header and I call the background() function onLoad. Can someone tell me what I'm doing wrong?
Mar 20 '08 #2
mrhoo
428 256MB
Why don't you use a css style element in the page for this?
Mar 20 '08 #3
acoder
16,027 Expert Mod 8TB
You can't use document.write after the page has loaded, because the document is closed for writing.

mrhoo's suggestion is the correct way of solving this, e.g.
Expand|Select|Wrap|Line Numbers
  1. var code = "<style>#content{background-image:url(/images/" + name + ".jpg);background-repeat:repeat-y;background-position:right;background-attachment:fixed;</style>";
becomes
Expand|Select|Wrap|Line Numbers
  1. var content = document.getElementById("content");
  2. content.style.backgroundImage = "url(/images/" + name + ".jpg);";
  3. content.style.backgroundRepeat = "repeat-y";
  4. // background-position = style.backgroundPosition, background-attachment = style.backgroundAttachment
Mar 22 '08 #4
Thank. I didn't know you could access the CSS styles from javascript.
Mar 24 '08 #5
acoder
16,027 Expert Mod 8TB
Thank. I didn't know you could access the CSS styles from javascript.
See what you can change.
Mar 24 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

31
by: Arthur Shapiro | last post by:
I'm the webmaster for a recreational organization. As part of one page of the site, I have an HTML "Calendar at a Glance" of the organization's events for the month. It's a simple table of a...
5
by: Derek Fountain | last post by:
I have a horizontal navigation bar, which is a single row table containing the right images. It "stretches" itself across the screen using a penultimate td like this: <td width="100%"...
0
by: Jeb Hunter | last post by:
Well, how can I describe this succinctly? I have a page with DIVs that us background images to produce a border effect. It works perfectly well, but I want to make up (for now) 3 different...
16
by: stevedude | last post by:
CSS newbie again. I have a problem trying to get coffee mug images within anchor tags to center with my link text for a vertical list menu. If I use the horizontal/vertical properties of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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?
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.