views:

53

answers:

2

Hi there,

I want to be able to include a dynamic chunk of HTML in a page using javascript.

I wrote a servlet which returns a chunk of HTML, http://mydomain.com/dynamic.jsp It produces some HTML ( minus head, body tags ) like this:

<div>
     Current Weather : 89F
</div>

How can I get that dynamic chunk of HTML into the page using a line of Javascript?

What is the simplest way? I wonder if I need to wrap what the servlet produces in XML tags or in JSON format.

+5  A: 

JQuery's .load() function is handy for loading data from a server, and you can dynamically put the HTML anywhere you want using the callback.

http://api.jquery.com/load/

a.feng
Note that this will only work if both scripts run on the same domain.
Ghommey
You are right, it works fantastic, except if I want to call a URL on another domain.
jeph perro
+1  A: 

If you want to use prototypejs, take a look here: http://www.prototypejs.org/learn/introduction-to-ajax

David Radcliffe
http://api.prototypejs.org/ajax/ajax/updater/ In particular.
Philip T.