Solved

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

Posted on 2007-04-07
2
3,475 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.

813 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

17 Experts available now in Live!

Get 1:1 Help Now