Skip to content

Instantly share code, notes, and snippets.

@indexzero
Created June 6, 2011 02:11
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save indexzero/1009644 to your computer and use it in GitHub Desktop.
Save indexzero/1009644 to your computer and use it in GitHub Desktop.
Code samples from the "jsdom and jquery" article on the Nodejitsu blog
var httpAgent = require('http-agent'),
util = require('util');
var agent = httpAgent.create('www.google.com', ['finance', 'news', 'images']);
agent.addListener('next', function (err, agent) {
console.log('Body of the current page: ' + agent.body);
console.log('Response we saw for this page: ' + util.inspect(agent.response));
// Go to the next page in the sequence
agent.next();
});
agent.addListener('stop', function (err, agent) {
console.log('the agent has stopped');
});
agent.start();
var httpAgent = require('http-agent'),
jsdom = require('jsdom');
var agent = httpAgent.create('www.google.com', ['finance', 'news', 'images']);
agent.addListener('next', function (err, agent) {
jsdom.env({
html: agent.body,
scripts: [
'http://code.jquery.com/jquery-1.5.min.js'
]
}, function (err, window) {
var $ = window.jQuery;
// jQuery is now loaded on the jsdom window created from 'agent.body'
console.log($('body').html());
agent.next();
});
});
agent.addListener('stop', function (agent) {
console.log('the agent has stopped');
});
agent.start();
var request = require('request'),
jsdom = require('jsdom');
request({ uri:'http://www.google.com' }, function (error, response, body) {
if (error && response.statusCode !== 200) {
console.log('Error when contacting google.com')
}
jsdom.env({
html: body,
scripts: [
'http://code.jquery.com/jquery-1.5.min.js'
]
}, function (err, window) {
var $ = window.jQuery;
// jQuery is now loaded on the jsdom window created from 'agent.body'
console.log($('body').html());
});
});
var jsdom = require('jsdom');
jsdom.env({
html: "<html><body></body></html>",
scripts: [
'http://code.jquery.com/jquery-1.5.min.js'
]
}, function (err, window) {
var $ = window.jQuery;
$('body').append("<div class='testing'>Hello World</div>");
console.log($(".testing").text()); // outputs Hello World
});
{
"name": "jsdom-jquery-example",
"description": "Code samples from the 'jsdom and jquery' article on the Nodejitsu blog",
"version": "0.2.0",
"author": "Charlie Robbins <charlie.robbins@gmail.com>",
"repository": {
"type": "git",
"url": "git://gist.github.com/1009644.git"
},
"keywords": ["jsdom", "jquery", "samples"],
"dependencies": {
"jsdom": "0.2.x",
"http-agent": "0.1.x",
"request": "1.9.x"
},
"engines": { "node": ">= 0.4.4" }
}
var request = require('request'),
sys = require('sys');
request({ uri:'http://www.google.com' }, function (error, response, body) {
if (error && response.statusCode !== 200) {
console.log('Error when contacting google.com')
}
// Print the google web page.
sys.puts(body);
});
curl -o jsdom-jquery.tar.gz https://gist.github.com/gists/1009644/download
tar -xvf jsdom-jquery.tar.gz
cd gist1009644*
npm install
node request.js
node jsdom-jquery.js
node jquery-request.js
node jquery-http-agent.js
node http-agent.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment