Solved

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
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.
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.

743 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