Solved

AJAX - Javascript in Image Onload Won't Run in Opera in an HTML Response

Posted on 2007-04-07
2
3,472 Views
Last Modified: 2013-12-07
I'm very new to AJAX and I'm trying to solve a problem that is probably pretty simple...

Sometimes I need a script to run based on a response I get from an HTTP request. I've thought of a lot of ways to do this programmatically by looking at the XML response. However, I've found a very simple way to do this using a regular HTML response.

In my response, I add a single-pixel image that has a randomly generated name (to prevent caching) that looks something like this:

<img src="1x1clear.gif" onload="JavaScriptFunctionINeedToRun();">

This works like a charm in IE 6+, FireFox 1.5+, and Safari. But for some reason Opera doesn't want to do that scrip on load.

So, my question is how can I get this to work in Opera? I'm open to a solution that uses my image idea, but I realize that the image idea may not be the best way so I'm also open to another solution.

Keep in mind, the goal is to make this very easy. I don't really want to have to parse commands out of an XML response or anything like that. The goal is to have a script run based on a regular HTML response that is rendered into my website HTML.

0
Comment
Question by:cnxmax
2 Comments
 
LVL 15

Accepted Solution

by:
stanscott2 earned 500 total points
ID: 18870044
This is a known bug in Opera and is, in fact, fixed in the beta 8.x version.

In the meantime, consider doing this:

BEFORE the Ajax handler fires, capture the image's src property.

var curSrc = document.getElementById("something").src;

AFTER the request has completed, check the image's current source against the value you stored previously:

if (curSrc != document.getElementById("something").src) {
    doSomething();
}

It's a hack, but it appears to be necessary at this time.
0
 

Author Comment

by:cnxmax
ID: 18870082
stanscott2,

Thank you for the response. It's good to know that the bug is in Opera (and that it will be fixed in future versions).

Like I said, I'm new at this so I'm might not be following correctly, but it looks like your solution requires me to already know the script that needs to run. The solution I'm looking for allows me to run the javascript function (and any variables I need to pass into it) to based on what's happening on the server.

For example, my image might return something like this:

<img src="1x1clear.gif" onload="JSFunctionChangePriceTo(394.94);">

or something like this:

<img src="1x1clear.gif" onload="JSFunctionShowUserInfo('Tom', 'Johnson', 1979, 'Level 1');">

So, while the actual function already on the page, the function that I call will have arguments that I put in there when the HTML rendered server-side.

I'm not as worried about making this work in Opera as I am about doing it in the most stable way, so I'm still open to any ideas that accomplish what I'm looking for in a better way.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Several part series to implement Internet Explorer 11 Enterprise Mode
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now