Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3660744
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:14:09+00:00 2026-05-19T01:14:09+00:00

What I have is a little jQuery based script intended to grayscale an image.

  • 0

What I have is a little jQuery based script intended to grayscale an image.

It works, but I have a big issue with picking the event that will trigger it.
I need it to simply be automatically triggered, once, when image is loaded. Now, this:

imgObj.ready

works perfectly in IE, in Firefox it’s a gamble on each refresh, and in Safari/Chrome/Opera not at all.

This however:

imgObj.click

works great in all browsers, but I need it to happen automatically, without clicking.
(or with emulated click, but I didn’t manage to make it, anyone knows how?)

And finally this:

imgObj.load

works “too good” – it works in all browsers, but keeps re-executing the function over and over – I tried putting a variable inside a function to make it be executed just once, but it didn’t work, .load keeps re-executing.

Sorry for the long post, and thanks very much for reading through – below is the full code.

    var imgObj = $('#image'); 

        imgObj.click(function(){

            var imgObj = document.getElementById('image');

            if($.browser.msie){
                grayscaleImageIE(imgObj);
            } else {
                imgObj.src = grayscaleImage(imgObj);
            }           
        });

    function grayscaleImageIE(imgObj)
    {
        imgObj.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(grayScale=1)';
    }

    function grayscaleImage(imgObj)
    {


        var canvas = document.createElement('canvas');
        var canvasContext = canvas.getContext('2d');

        var imgW = imgObj.width;
        var imgH = imgObj.height;
        canvas.width = imgW;
        canvas.height = imgH;

        canvasContext.drawImage(imgObj, 0, 0);
        var imgPixels = canvasContext.getImageData(0, 0, imgW, imgH);

        for(var y = 0; y < imgPixels.height; y++){
            for(var x = 0; x < imgPixels.width; x++){
                var i = (y * 4) * imgPixels.width + x * 4;
                var avg = (imgPixels.data[i] + imgPixels.data[i
+ 1] + imgPixels.data[i + 2]) / 3;
                imgPixels.data[i] = avg; 
                imgPixels.data[i + 1] = avg; 
                imgPixels.data[i + 2] = avg;
            }
        }

        canvasContext.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
        return canvas.toDataURL();      
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:14:10+00:00Added an answer on May 19, 2026 at 1:14 am

    How about this?

    imgObj.one('load',function(){
    
            var imgObj = document.getElementById('image');
    
            if($.browser.msie){
                grayscaleImageIE(imgObj);
            } else {
                imgObj.src = grayscaleImage(imgObj);
            }           
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a teeny tiny little issue concerning a jQuery-based slideshow in Opera. What
I have been trying to make this to be a little jQuery plugin that
I have a little problem with some jquery and http://www.mikage.to/jquery/jquery_history_noc.html The function works great,
I'm a jQuery newbie, and I have trouble with a little script I created.
I have a a login in script that is a little to complicated for
I have this little jQuery slideDown effect http://jsfiddle.net/Gg4eP/2/ But the animation isn't smooth? What
I have written a little JQuery / Javascript add on for our form, that
I have a django-based web application that is a mixture of jQuery and several
So I've got this UpdatePanel. Inside it I have a nifty little jQuery scroll
I have a little problem with Jquery getJSON function. my json here { entries:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.