Tutorial :Is there a way to mitigate downloading of resources (images/css and js files) with javascript?


I have a html page on my localhost - get_description.html.

The snippet below is part of the code:

<input type="text" id="url"/>  <button id="get_description_button">Get description</button>  <iframe id="description_container" src="#"/>  

When the button is clicked the src of the iframe is set to the url entered in the textbox. The pages fetched this way are very big with lots of linked files. What I am interested in the page is a block of text contained in a <div id="description"> element.

Is there a way to mitigate downloading of resources linked in the page that loads into the iframe?

I don't want to use curl because the data is only available to logged in users and the steps to take with curl to get the content is too complicated. The iframe is simple as I use this on a box which sends the right cookies to identify the request as coming from a logged in user, but the problem is that it is very wasteful to get nearly 1 MB of data to keep 1 KB of it and throw out the rest.


If the proposed method just works in Firefox it is fine, so I added firefox tag. Also, it is possible that the answer actually is from the realm of Firefox add-on techniques, so I added that tag as well.

The problem is not that I cannot get at what I'm looking for, rather, the problem is the easy iframe method is wasteful.

I know that Firefox does allow loading only the text of a page. If you open a page and press Ctrl+U you are taken to 'view page source' window, There links behave as normal and are clickable, if you click on a link in source view, the source of the new page is loaded into the view source window, without the linked resources being downloaded, exactly what I'm trying to get. But I don't know how to access this behaviour.

Another example is the Adblock add-on. It somehow kills elements before they get loaded. With plain javascript this is not possible. Because it only is triggered too late to intervene in good time.


Actually I've seen Cross Domain jQuery .load request before, here: http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/

The author claims that codes like these found on that page

$('#container').load('http://google.com'); // SERIOUSLY!    $.ajax({      url: 'http://news.bbc.co.uk',      type: 'GET',      success: function(res) {          var headline = $(res.responseText).find('a.tsh').text();          alert(headline);      }  });    // Works with $.get too!  

would work. (The BBC code might not work because of the recent redesign, but you get the idea)

Apparently it is using YQL wrapped into a jQuery plugin to do the trick. Now I cannot say I fully understand what he is doing there but it appears to work, and fits the bill. Once you load the data I suppose it is a simple matter of filtering out the data that you need.

If you prefer something that works at the browser level, may I suggest Mozilla's Jetpack framework for lightweight extensions. I've not yet read the documentations in its entirety but it should contain the APIs needed for this to work.


The Same Origin Policy forbids any web page to access contents of any other web page in a different domain so basically you cannot do that.

However it seems that with some browsers it is allowed to access web pages content if you are trying to access it from a local web page which seems to be your case.

Safari, IE 6/7/8 are browser that allow a local web page to do so via XMLHttpRequest (source: Google Browser Security Handbook) so you may want to choose to use one of those browsers to do what you need (note that future versions of those browsers may not allow to do so anymore).

A part from this solution I only see two possibities:

  • If the web pages you need to fetch content from are somehow controlled by you, you can create a simpler interface to let other web pages to get the content you need (for example allowing JSONP requests).
  • If the web pages you need to fetch content from are not controlled by you the only solution I see is to fetch content server side logging in from the server directly (I know that you don't want to do so, but I don't see any other possibility if the previous I mentioned are not practicable)

Hope it helps.


There are various ways to go about this in AJAX, I'm going to show the jQuery way for brevity as one option, though you could do this in vanilla JavaScript as well.

Instead of an <iframe> you can just use a container, let's say a <div> like this:

<div id="description_container"></div>  

Then to load it:

$(function() {    $("#get_description_button").click(function() {      $("#description_container").load($("input").val() + " #description");    });  });  

This uses the .load() method which takes a string in this format: .load("url selector"), then takes that element in the page and places it's content inside the container you're loading, in this case #description_container.

This is just the jQuery route, mainly to illustrate that yes, you can do what you want, but you don't have to do it exactly like this, just showing the concept is getting what you want from an AJAX request, rather than in an <iframe>.


Your description sounds like you are fetching pages from the same domain (you said that you need to be logged in and have session credentials) so have you tried to use async request via XMLHttpRequest? It might complain if the html on a page is particularly messed up but you chould still be able to get raw text via .responseText and extract what you need with a regex.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »