Solved

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

Posted on 2007-04-07
2
3,482 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

#Citrix #Internet Explorer #Enterprise Mode #IE 11 #IE 8
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…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

751 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