Your code has a syntax error in it. Install Firebug in your Firefox browser
and enable it for your page. Then reload the page and you'l get an error
message. It's referring to is the {} in the .replaceWith({...}) call.

But fixing that won't help, because the logic is wrong. The code seems to be
assuming that jQuery.ajax({...}) will return the requested data as its
return value. It doesn't do that. The data you request isn't ready when
.ajax() returns. Instead, it is passed as an argument to the success:
function.

Fixing that won't help either, because the URL you're requesting doesn't
return anything you can use in this manner.

Open your weather URL directly in a browser and do a View Source:

http://weather.ibegin.com/js/us/ak/ninilchik/0/0/1/1/0/custom.js&background_
color=transparent&color=093384&width=200&padding=0&border_width=0&border_col
or=transparent&font_size=18&font_family=inherit&showicons=1

You will see that the URL doesn't return an HTML fragment suitable for
insertion into the DOM with .replaceWith(). Instead, it returns JavaScript
code consisting of a bunch of document.write() calls.

The way this widget is normally used is that you pass that URL as the src=""
attribute of a <script> tag, correct?

You would need to execute this script, which would be easy enough to do with
an eval(), but that won't help (are you tired of me saying that?), because
the document.write() calls *must* be made during the initial page load, e.g.
from a <script> tag in your document.

But there's another problem. You're trying to do a .ajax() GET across
domains. You can't do that.

Basically, unless the widget provider offers another format for their
widget, you need to do it their way: Use that URL in a <script
type="text/javascript" src="..."></script> just like they say to do.

The one viable option if you want more control would be to create a second
.html file which is a complete page that loads the widget in that manner. In
your main page, use an <iframe src="thatfile.html"></iframe> to include that
second .html file as an IFRAME.

Once you get that working, you can add additional JS code inside the IFRAME
to detect whether the widget was loaded successfully and hide it if not.

But what does "not loaded successfully" mean here? If the weather site is
down, you may get a long timeout before you are able to figure out it wasn't
loaded. Then if you hid it, you'd have a blank box appear on your page which
would then disappear some number of seconds after the page loads. You'd
probably want to substitute some message instead of that.

-Mike

> -----Original Message-----
> From: jquery-en@googlegroups.com 
> [mailto:[EMAIL PROTECTED] On Behalf Of bcbounders
> Sent: Sunday, September 07, 2008 2:56 PM
> To: jQuery (English)
> Subject: [jQuery] Re: Determine if a Javascript has loaded?
> 
> 
> OK... so here's what I've got so far.  I'm trying to work out 
> the jQuery so that if the iBegin weather widget is 
> successfully loaded, then the <div id="weather"> is shown... 
> otherwise, it's hidden.
> 
> <script type="text/javascript">
>       jQuery.ready(function(){
>               jQuery("#weather").replaceWith({
>                       jQuery.ajax({
>                               type: "GET",
>                               dataType: "script",
>                               url: 
> "http://weather.ibegin.com/js/us/ak/ninilchik/0/0/1/1/0/
> custom.js&background_color=transparent&color=093384&width=200&
> padding=0&border_width=0&border_color=transparent&font_size=18
> &font_family=inherit&showicons=1",
>                               success: function(){
>                                               
> jQuery("#weather").show()
>                                       },
>                               error: function(){
>                                               
> jQuery("#weather").hide()
>                                       }
>                       });
>               });
>       });
> </script>
> 
> But... what I've got isn't working.  And, being new to jQuery 
> (and Javascript), I'm not sure why it's not working... or 
> even if this is the best route to try and achieve what I want.
> 
> Can someone take a look and provide some feedback?  I'd 
> really appreciate it!
> 
> Thanks a lot!
> 
>  - John
> 
> On Sep 6, 10:49 am, bcbounders <[EMAIL PROTECTED]> wrote:
> > Rene,
> >
> > Thanks so much for posting.
> >
> > I got the site up, using the iBegin weather widget on it's 
> own.  If it 
> > helps, here's a link:  http://tinyurl.com/5n8mco
> >
> > I'll take a stab at doing what you suggest... wish me luck! 
> :D  But...
> > expect to hear more questions from me soon.
> >
> >  - John
> 

Reply via email to