1

$('#loadHere').load('https://www.google.com'); - At present, this loads the Google homepage in <div id="loadHere"></div>.

What I would like to do is somehow store that output as an HTML string in a var instead.

Pseudo-code: var googleString = parseString($.load('https://www.google.com'));

When googleString is output, it would spit out <html>...<head>...<body>...</html>.

Is this possible?

My ultimate intention is to search through the string for a particular id.

Thank you kindly.

Index.html:

<!--snip-->
<script src="https://.... jQuery file"></script>
<div id="loadHere"></div>
<script src="changePage.js"></script>
<!--snip-->

changePage.js

$(document).ready(function(){
$('#loadHere').load('https://www.google.com');
}

[Edit] - Included snippets of the two files.

taylorswiftfan
  • 1,371
  • 21
  • 35
  • _"`$('#loadHere').load('https://www.google.com');` - At present, this loads the Google homepage in `
    `."_ How were you able to achieve this?
    – guest271314 Aug 31 '16 at 01:40
  • Possible duplicate of [Can Javascript read the source of any web page?](http://stackoverflow.com/questions/680562/can-javascript-read-the-source-of-any-web-page) – HPierce Aug 31 '16 at 02:09

2 Answers2

5

The load method of jQuery is a shorthand for the jQuery ajax method. The load method executes the ajax method and outputs the result in an element you provide.

Now, what you would want to do is execute a post or get (or essentially an "ajax") jQuery method that has a success callback.

$.get( "https://www.google.com", function( data ) {
    var result = data;
    console.log(data);
});

See:

Klaassiek
  • 2,795
  • 1
  • 23
  • 41
0

You could do that upfront with the load method. From the jquery documentation:

$( "#b" ).load( "article.html #target" );

or for your case

$('#loadHere').load('https://www.google.com #specialDivYoureLookingFor');

If you don't want to do that, you could make the .load return a jquery object then search from there.

$($('#loadHere').load('https://www.google.com')).find('#specialDivYoureLookingFor');
Eddimull
  • 342
  • 1
  • 8