1

Possible Duplicate:
Using Javascript in CSS

is it possible to use javascript codes in css background?
mean something like this :

style="background:url(javascript:---javascript codes---);" does this syntax work or not?

thanks in advance

Community
  • 1
  • 1
SilverLight
  • 19,668
  • 65
  • 192
  • 300

3 Answers3

3

You cant use JavaScript inside CSS. But if you want to change the background image using JavaScript following line will work,

document.getElementById("<element ID>").style.backgroundImage = "url(<url of image>)";
Madurika Welivita
  • 890
  • 1
  • 10
  • 19
2

Accessing the Style Attribute with JavaScript

You cannot use JavaScript in your CSS, but you can use JavaScript to set your CSS rules. For instance:

// Create CSS BackgroundImage Rule
var bg = "url('http://placekitten.com/200/300)";

// Apply Rule
document.getElementById("foo").style.backgroundImage = bg;

Making Variables and Functions Possible in CSS

Additionally, there are a series of frameworks and tools that permit you to use functions and variables in CSS, such as SASS or LESS.

IE's Old (and dead since 2008) Dynamic Properties

The closest you'll get to having JavaScript in your CSS would be something Dynamic Properties (introduced in IE5), which have very poor support cross-browser, and are no longer supported in modern versions of IE.

Quick and Easy CSS Modification with jQuery

If you need to do a lot of modifications to your styles with JavaScript, by and large people are moving towards adopting frameworks to assist with that. For instance, one popular framework is jQuery, which permits things like:

$("#foo").css('background-color', 'red');

As well as mass-assignment through objects:

var color  = "#ffe", 
    border = "5px solid #ccc";

$("#foo").css({ 'background-color': color, 'border-left': border });
Sampson
  • 265,109
  • 74
  • 539
  • 565
1

Technically yes, with an expression:

background: expression(JS code resulting in a vali background image string here)

However I think it only works in IE and it's very bad practice because it may have to re-evaluate the code hundreds of times every second.

Instead, you should have a suitable default there, and then use JavaScript later on the page to change it.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • thanks for the answer / would you please give a workable example! – SilverLight May 20 '12 at 16:43
  • It should be noted that expressions have been unsupported since 2008, and don't work in modern versions of IE. I mean no offense, but I'm baffled by the fact that this was the *accepted* answer. – Sampson May 21 '12 at 00:28
  • @JonathanSampson I didn't expect it to be the accepted answer either. I honestly expected it to be made a comment... But if it's for an intranet where the computers are on older IE (as is the unfortunate case of many companies) then it's perfect. – Niet the Dark Absol May 21 '12 at 01:51
  • @Kolink That would be one scenario, though I wouldn't consider it *perfect* in that it further cements them into being unable to upgrade their browser at any time in the future :) – Sampson May 21 '12 at 01:56