views:

37

answers:

5

So I'm working on making a dynamic drop down select form and I need for each menu to propagate possible choices from a prebuilt chunk of html (located at, say, http://example.com/menu/choices) is there an easy way to use javascript to fetch the html of a remote page and then plug that in to the page? I know I can use .write() to insert the code, I just don't know how to fetch it.

Thanks!

EDIT: The code in question is dynamic and is generated from different inputs. For example, one such page is http://ohiostate.bncollege.com/webapp/wcs/stores/servlet/TBDropDownView?jspStoreDir=ohiostate&deptId=&campusId=30747395&catalogId=10001&dojo.transport=xmlhttp&termId=44357427

I want to access the available college courses it generates.

+1  A: 

The thing you want is called AJAX (Asynchronous JavaScript and XML). If you're using jQuery:

$('#my-select-thingy').load('select-options.cgi');

or whatever flavour of server-side you prefer. You should have something like this in HTML:

<div id="my-select-thingy">
  <!-- select will go here -->
</div>

and the url above should return something like this:

<select>
  <option>Foo</option>
  <option>Bar</option>
</select>
Amadan
Awesome! Thanks much for that snippet! I'm guessing that this is jQuery?
Parker
@Parker: That is indeed `jQuery`. It will also be hampered by the Same Origin Policy. Whether that's a problem for what you're trying to do is another matter. :-)
T.J. Crowder
Yes, jQuery. All other JavaScript libraries of note today have similar functionality though; coding it from scratch is a major pain because of browser incompatibilities.
Amadan
Parker
If you have control of your own server, you can trick your browser: at your server, pull data from the remote server, and serve it to your page. This way, the browser is still only communicating with your server. Be nice about setting a proper `UserAgent`; it would also be good to have a permission to do so (or at least not have a policy forbidding automated page accesses), otherwise you might get an IP-block.
Amadan
+1  A: 

You're way better of using jQuery for that. Just look into the Ajax methods and you'll get the hang of it. http://api.jquery.com/category/ajax/

Claudiu
+1  A: 
T.J. Crowder
Nope, I put it in in the beginning :) and can I use jQuery to access html? like getHTML or something? It would just be simple source code.
Parker
@Parker: Sure. My first read of your question was that it was about *code*, but I've updated to address loading markup as well. With live examples.
T.J. Crowder
@TJ: Fantastic answer, I chose you for your fast updates, extensive explanations, and live examples. Thanks much :)
Parker
@Parker: No worries, glad that helped!
T.J. Crowder
+1  A: 

Javascript usually can't access other websites for security reasons. If we could load content from wherever we wanted with a script we'd see some pretty rampant chaos. A simple solution is an iframe with the other document or just a section of it.

Does the website have anyway for you to access that info? If you can find an interface you can just get the info and stick in in the document. Otherwise you'd have to do some scraping.

CrazyJugglerDrummer
How could I use greasemonkey? And yes, if theres a way I could enmass get the code for a site that would be great1
Parker
There's no problem adding scripts from other sites dynamically. In fact, that's how [JSONP](http://en.wikipedia.org/wiki/JSONP#JSONP) works to get around the Same Origin Policy.
T.J. Crowder
@Parker greasemonkey is a way to run javascript in your browser after a page has downloaded, but it only works on your machine. If that's what your going for then you'd just need to create a script that scraped the data.
CrazyJugglerDrummer
+1  A: 

Suppose you have the following HTML markup

<div id="fakeDiv"></div>

you can execute an ajax request like this

$.ajax({
    type: "get",
    dataType: "html",
    url: "http://example.com/menu/choices",
    data: {},
    success: function(response) {
        $("#fakeDiv").html('').html(response);
    },
});

to inject the html code returned by your url inside the DIV element.

This is jQuery code. Hope it helps!

Lorenzo
Thanks Lorenzo! Can I do this with pages that aren't on my own server?
Parker
@Parker: you're welcome. I've never done but I think you can do it without problems.
Lorenzo