Solved

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

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Open dialog with server side controls in it 3 46
Provide the IDEA of External JS 1 35
Link not working 6 41
How do I post more than 1 item to php backend 24 27
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

713 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