Is it possible to get the HTML of a div from another page in a variable as a string, so I can run a regex search to find a specific number?
-
It sure is, but on the clientside only, it probably isn't ? – adeneo Dec 29 '14 at 17:59
-
from same domain yes, from other domain you'll need to proxify it server side – A. Wolff Dec 29 '14 at 18:00
-
possible duplicate of http://stackoverflow.com/questions/4560183/load-content-from-external-page-into-another-page-using-ajax-jquery?rq=1 – Dec 29 '14 at 18:00
2 Answers
If you're loading content from the same domain, the answer @lawrence overflow linked to in his comment will do the trick:Load content from external page into another page using Ajax/jQuery
JS with jQuery:
$(document).ready(function() {
$("#main").load('sourcePage.html #content');
});
Otherwise, you'll need to use server-side technology. Here's a Node.js server that proxies for another site:
JS (Node and Express):
var request=require('request');
var express = require('express');
var app = express();
//Put the source URL here:
var URL='http://www.nytimes.com';
app.get('/', function (req, res) {
res.type('.html');
request(URL,function(err,response,body){
res.send(body);
});
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('listening at http://%s:%s', host, port);
});

- 1
- 1

- 14,346
- 12
- 59
- 97
-
instead of wrapping around the head how to install Node and stuff, one line of PHP and AJAX (standard jQuery snip) is quite enough. – Roko C. Buljan Dec 29 '14 at 18:26
-
I wanted to show how to clip the page to a certain region and also how to make the solution more flexible. Plus, this was tagged as a JavaScript question, so I gave a JavaScript answer. Could you give your PHP solution? – Max Heiber Dec 29 '14 at 18:36
-
@Roko C. Buljan: I simplified the code in response to your comments. Fair enough. – Max Heiber Dec 29 '14 at 18:53
-
Mheiber, Here's my PHP solution, which basically differs by just one line of PHP. The AJAX is basically the same. The solution can be seen here: http://stackoverflow.com/questions/14999573/jquery-load-external-site-page (upvoted your answer for the shown effort!) – Roko C. Buljan Dec 29 '14 at 19:01
-
@RokoC.Buljan thanks for the elegant PHP solution and upvote. I'm sure there's a simpler Node solution, but I'm too used to Express. – Max Heiber Dec 29 '14 at 19:05
If that other page is in your own domain:
jsBin demo
$('<div />').load("otherpage.html", function(data){
var num = /\d+/.exec( $(this).find("#number").text() );
console.log( num ); // 45
});
Note: the above presumes the desired number is somewhere inside the #number
element. The regex /\d+/
is used to get all the numbers from that element.
If the page is not in your domain:
jQuery load external site page
you'll first need to get that page content using PHP with file_get_contents
. After the desired content is on your server you'll not run any more into security issues and you can than respond to AJAX with the grabbed content.

- 1
- 1

- 196,159
- 39
- 305
- 313