views:

51

answers:

4

I have a web page that loads an external chat script, but the script loads before all my CSS and jQuery kick in, so my page looks all messed up for a few seconds. Need help figuring out how to launch my chat script once the page is done loading...I think it can be done by adding the script into my jQuery setup, but I'm not sure how to do that.

here's the code, it's self-explanatory enough not to be familiar with the actual software.

<script>
var online_text = "Chat Online Now";
var offline_text = "Chat Not Available";
var check_back = 1; // this lets the script check the operator status without being logged in
</script>
<script src="http://my.domain/chat/file.php"&gt;&lt;/script&gt;

I think what happens is that the .php file will write my online or offline text with the necessary chat link, and that's why I have to put the script in the place where I want the chat text to display.

hope that makes sense...and hopefully the community can help me figure out how to solve the problem. thanks!

UPDATE: here's where I am so far, unfortunately the #chaticon element is empty, even if I replace the script with simple text. Using jQuery 1.4.2 by the way:

$(document).load(function() { 
var online_text = 'Chat Online Now'; 
var offline_text = 'Chat Not Available'; 
var check_back = '1'; 
$("li#chaticon").html('<script type="text\/javascript" src="http:\/\/my.domain\/chat\/file.php"><\/script>');
});
+1  A: 

Check out using the JQuery function $(document).ready().

It will hold off running until your entire document is loaded and ready to go.

Dave Markle
It sounds like he needs the style sheets and additional scripts to actually be loaded first, which $(document).ready() won't wait for. Did you mean $(document).load()?
jmar777
A: 

$(document).ready(), which was previously mentioned, will only wait until the DOM is ready for manipulation (i.e., the node hierarchy has been fully constructed). This will not, however, wait until any additional resources (images, style sheets, etc.) have been loaded, which sounds like what you're wanting to do.

If you need all of these additional resources to be loaded first, then use $(document).load(). This will wait until all additional resources have finished loading (and hence the page will have been fully rendered).

jmar777
thanks! So where do I put the chat script itself? I'm thinking I enclose the three variables and the script into a function, then call that function where I want the chat icon to load up on the page, but enclose it with $(document).load(). of course, how to actually write that out is beyond me :)
Stephan Hovnanian
It depends a bit on how this other script loads the chat module. And is your /chat/file.php file actually emitting JavaScript (with the right mime type as well)? You mentioned that the placement of the script tag was important as well (had to be where the text should be rendered), which sounds like it may be doing a document.write()? It would be very helpful to either see the chat script, or at least have a better understanding of how it works.
jmar777
thanks. fist, I've realized that there could be problems loading the file using the native .ajax functions in jQuery because it's an external URL (on my server, but still uses the http:// protocol). As for the document.write() part, yes, I looked through the file and it seems that there's a bunch of older-model javascript going on. I don't think this is going to be as feasible as I'd like it to be, but I was able to improve performance just a bit by rewriting the script code in PHP, which would be run before most of the page loads.
Stephan Hovnanian
You can still use fully resolved requests with $.ajax (e.g., http://my.domain...), as long it's on the same domain that served the container page. I'm still not sure that I understand what's going to happen inside of your file.php though. Is it a mix of JavaScript and markup being emitted? If so, you're going to have a lot of trouble with that. You'd be better off simply loading some markup, and attaching event handlers unobtrusively after it's in the DOM. Browsers (especially IE), don't like dynamically loading HTML with embedded script tags. Doubly bad if they contain document.write()...
jmar777
If I can make a suggestion, I think you'd be better off by re-evaluating your needs with the chat module. If you need this level of dynamism, you might want to consider writing it with more of a plugin architecture... e.g., $('#mylink').click(function() { $('#some-container').chatify({ /* opts */ }); });
jmar777
A: 

Stephen,

As per Dave's advice of putting the $(document).ready() function on the page i.e:

$(document).ready(function() {
    var online_text = "Chat Online Now";
    var offline_text = "Chat Not Available";
    var check_back = 1;
    // do any other stuff here
});

also, make sure the js files are loaded at the bottom of the page, just ahead of the closing </body> tag and the css is loaded in the <head> tag. This may or may not sort the above issue, but is recognised as 'good practice' in terms of page configuration.

jim

jim
A: 

thanks jmar777, I agree there's some re-evaluating that needs to be done here...the chat script is a mix of document.write, php/mysql and some dom-oriented stuff. Not worth toying with for hours.

So I ended up putting the code from my original post into a PHP function and returning it to a variable all the way at the top of the page, this way it would be written a little bit earlier than if it were written on the fly while my page were loading. And I made a few tiny tweaks to my CSS file so that the part of the page that was getting strange-looking while the chat script loaded wouldn't look as strange.

Stephan Hovnanian