471,601 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,601 software developers and data experts.

Hello World with YUI Library - asyncRequest

Hi, here I have a very simple 'hello world' example that makes use of
Yahoo! User Interface Library. I have a problem with my response string
coming back as '[object, Object]'. I managed to check that everything
else works.

The requested 'SayHello.html' file only contains the string "hello,
world".

Here is my code:

<html>
<head>
<title>Hello World with YUI</title>
<script src="yahoo.js"></script>
<script src="connection.js"></script>
<script language="JavaScript" type="text/javascript">
var handleSuccess = function(o){
if(o.responseText !== undefined){
document.getElementById('span_result').innerHTML = o.responseText;
}
}

var handleFailure = function(o){
document.getElementById('span_result').innerHTML = 'ERROR: ' +
o.responseText;
}

var callback =
{
success:handleSuccess,
failure: handleFailure
};
</script>
</head>
<body>
<!-- Clicking this link initiates the asyncronous request -->
<a href="javascript:YAHOO.util.Connect.asyncRequest(' GET',
'SayHello.html', callback);">Say Hello</a><br>
<!-- used to display the results of the asyncronous request -->
<span id="span_result"></span>
</body>
</html>

Aug 9 '06 #1
3 5975
Hi again, I found what was wrong with my test page.

At the bottom of the page where I have the Anchor link I had this:

<a href="javascript:YAHOO.util.Connect.asyncRequest(' GET',
'SayHello.html', callback);">Say Hello</a><br>

I replaced it with this:

<a href="#" onclick="javascript:YAHOO.util.Connect.asyncReques t('GET',
'SayHello.html', callback);">Say Hello</a><br>

This prevents the browser from navigating away from the page and
instead you just handle the 'onclick' event.

Cheers,
SV.

Aug 10 '06 #2
sa********@gmail.com wrote:
Hi again, I found what was wrong with my test page.

At the bottom of the page where I have the Anchor link I had this:

<a href="javascript:YAHOO.util.Connect.asyncRequest(' GET',
'SayHello.html', callback);">Say Hello</a><br>

I replaced it with this:

<a href="#" onclick="javascript:YAHOO.util.Connect.asyncReques t('GET',
'SayHello.html', callback);">Say Hello</a><br>

This prevents the browser from navigating away from the page and
instead you just handle the 'onclick' event.
The browser will still "browse" to #.
It's even better when you replace it with this:

<a href="#" onclick="YAHOO.util.Connect.asyncRequest('GET',
'SayHello.html', callback); return false;">Say Hello</a><br>

or

<a href="noscript.html" onclick="YAHOO.util.Connect.asyncRequest('GET',
'SayHello.html', callback); return false;">Say Hello</a><br>
Aug 11 '06 #3
Robert wrote:
sa********@gmail.com wrote:
<snip>
><a href="#"
onclick="javascript:YAHOO.util.Connect.asyncReque st('GET',
'SayHello.html', callback);">Say Hello</a><br>

This prevents the browser from navigating away from the page and
instead you just handle the 'onclick' event.

The browser will still "browse" to #.
It's even better when you replace it with this:
I suspect that "handle the 'onclick' event" could mean; call -
preventDefault - and/or setting - returnValue - on the event object, so
returning false would only be necessary for back-compatibility (even if
the back-compatible method is reliable on all modern browsers).
<a href="#" onclick="YAHOO.util.Connect.asyncRequest('GET',
'SayHello.html', callback); return false;">Say Hello</a><br>

or

<a href="noscript.html"
onclick="YAHOO.util.Connect.asyncRequest('GET', 'SayHello.html',
callback); return false;">Say Hello</a><br>
The problem with that, and certainly the latter, is that if the method
calls fails, or cannot act (such as when IE 5-6 is set up with ActiveX
disabled in the Internet security zone and the page is on the Internet),
the onclick event is still cancelled. An alternative noscirpt.html page
may be a sensible response to failure due to the browser not supporting
scripting, but there should be an equally sensible response to the
browser not supporting the action attempted by the script (and they will
likely be the same response (so long as the former is not haranguing the
user for not having javascript available)).

The only code that is in a position to asses the viability of the
actions it is attempting would be the function/method called from the
onclick handler (or the handler itself) so the optimum pattern should
be - onclick="return doSomethingFun('parameter');" - and have the
function/method return true or false to signal its success.

Of course general purpose libraries don't know how they are going to be
used and so will not tend to be designed with such methods signalling
their success in such a way, or designed with any consideration of clean
degradation at all.

Richard.

Aug 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

24 posts views Thread by Andy Sutorius | last post: by
17 posts views Thread by Tim Judd | last post: by
6 posts views Thread by Matthew | last post: by
33 posts views Thread by ankursinha | last post: by
26 posts views Thread by Santanu Chatterjee | last post: by
1 post views Thread by petermichaux | last post: by
1 post views Thread by James T. Dennis | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.