0


I'm having a lot of problems with IE7/IE8 and Javascript/jQuery, which I don't have in Chrome or FireFox.
I have this Javascript function:

function changeImg(img,bla){
    $('#open_case').css("background-image", "url('"+img+"')");
    getIngo(bla);
    navigation_image(bla);
}

And this is my onclick function:

<div class="cImage" style="background-image:url('http://bla.com/images/this_image.jpg');" onclick="changeImg('http://bla.com/images/this_image.jpg','2');"></div>

But it's like the function isn't called at all, 'cause if I change the changeImg function to an alert function:

function changeImg(img,bla){
    alert('hi!');
}

It still doesn't work.
The only error IE7/IE8 gives is Expecting Object(roughly translated)
What am I doing wrong?
Thanks in advance!

[Edit:]
These are lines IE7/IE8 is pointing at;

<script type="text/javascript" src="Scripts/swfobject.js"></script>
<script type="application/javascript" language="javascript">
function clearText(textfield){
    if (textfield.defaultValue == textfield.value){
        textfield.value = '';
        $(textfield).css('color','#000000');
    }
}
function addText(textfield){
    if (textfield.value == ''){
        textfield.value = textfield.defaultValue;
        $(textfield).css('color','#999999');
    }
}
Jay Wit
  • 2,987
  • 7
  • 32
  • 34
  • what is the line number of the error you are getting in IE – Ibu May 23 '11 at 09:24
  • Is your script tag in the head? Try using jQuery to bind the event rather than puting it in onclick="". $(document).ready(function(){$('.cImage').live('click',function(){changeImg('http://bla.com/images/this_image.jpg','2');});}); – Emyr May 23 '11 at 09:25
  • What does `getIngo(bla);` and `navigation_image(bla);` do? Or is it irrelevant to the error? – Alex R. May 23 '11 at 09:26
  • @Ibu; At line 1, char 1; and after that line 10 till 15, char 1. Line 10 is a totally different function, I'll add the function to my post. - @Emyr; Yes it is in the head, so just place that code in the onclick? – Jay Wit May 23 '11 at 09:29
  • what is the version of IE?? try `onClick="javascript:changeImg('http://bla.com/images/this_image.jpg','2');"` – xkeshav May 23 '11 at 09:32
  • @diEcho; IE7 and IE8, and it doesn't work – Jay Wit May 23 '11 at 09:36
  • @Alex R.; These are separate (Ajax) functions, they are irrelevant, there are no differences when I remove them. – Jay Wit May 23 '11 at 09:37

2 Answers2

3

If the error is object expected. Then try this

Change your

<script type="application/javascript" language="javascript">

To

<script type="text/javascript">
ace
  • 6,775
  • 7
  • 38
  • 47
  • You sir, were totally right! Could you explain me why it still worked in Chrome? Thanks a lot! – Jay Wit May 23 '11 at 09:57
  • 2
    I also not sure why maybe because the version of JavaScript they are using, take a look on this links http://stackoverflow.com/questions/300185/google-chrome-javascript-version and https://developer.mozilla.org/en/Chrome_tests – ace May 23 '11 at 10:05
2

Is there a reason you're using javascript here? It looks like you're changing the image on a navigation item? You could just just the CSS :hover classes. Regardless, using onmouseover isn't really the "proper" way to be handling this sort of thing in javascript, if you really cannot use just CSS, at the very least look into handling the mouse over envent properly in jQuery.

Dunhamzzz
  • 14,682
  • 4
  • 50
  • 74
  • @Dunhamzzz; There are navigation buttons. But when you click on one of them, the background of a div has to change, so hover won't do the trick :). – Jay Wit May 23 '11 at 09:33
  • 1
    you can change the background on hover @Jay – Ibu May 23 '11 at 09:36
  • Ok, at the very least look into the jquery click events, with your current setup you are using old javascript methods with jQuery. You could set a click event handler which does toggles a CSS class to change the image. Put both toggle images on the same image and use background-position in a class to move the image. – Dunhamzzz May 23 '11 at 09:37
  • @Ibu; it changes the background of a parent div, I don't think thats possible in CSS, also it has to be an onclick. – Jay Wit May 23 '11 at 09:40
  • @Dunhamzzz Sorry but could you explain it a little more, I don't quite get it. – Jay Wit May 23 '11 at 09:41
  • 1
    on your next question when you ask how to change the background image with a hover, i will be very glad to answer – Ibu May 23 '11 at 09:42
  • Read up on [CSS Sprites](http://www.alistapart.com/articles/sprites) this is as far as I'm gonna go with this one sorry. – Dunhamzzz May 23 '11 at 09:46
  • 1
    @Jay I think Ace will answer your question, I was trying to help you do it in a better way. – Dunhamzzz May 23 '11 at 09:57