Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Code samples from the "jsdom and jquery" article on the Nodejitsu blog

View http-agent.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
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();
View http-agent.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
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();
View http-agent.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
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());
});
});
View http-agent.js
1 2 3 4 5 6 7 8 9 10 11 12 13
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
});
View http-agent.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
{
"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" }
}
View http-agent.js
1 2 3 4 5 6 7 8 9 10 11
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);
});
View http-agent.js
1 2 3 4 5 6 7 8 9
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
Something went wrong with that request. Please try again.