By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,652 Members | 1,451 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,652 IT Pros & Developers. It's quick & easy.

store source code in a variable

P: 83
Hi,

I can easily display the page source by simply calling

window.location = "view-source:http://www.example.com";

but how can i store the source code into a variable which i can work with.

I don;t want to use ajax as i will get the cross domain problem.

Thanks
Nov 22 '09 #1
Share this Question
Share on Google+
2 Replies


P: 62
You could only do that yourself... possibly automatically (using SendKeys or using a .hta file extension locally), but you cannot create a website that does that (browsers know what zone the user is in)
Nov 24 '09 #2

Dormilich
Expert Mod 5K+
P: 8,639
to work with such a page, wouldn’t it be easier to load that document?

once you have that it’s easy to get the source as string
Expand|Select|Wrap|Line Numbers
  1. // though the intended purpose of this is somewhat specialized…
  2. Element.prototype.getString = function()
  3. {
  4.     var tag = this.tagName;
  5.     var str = "<" + tag;
  6.     var atb = this.attributes;
  7.     for (var l, i=0, l=atb.length; i<l; i++) {
  8.         if (i in atb) {
  9.             str += " " + atb[i].name + '="' + atb[i].value + '"';
  10.         }
  11.     }
  12.     var cn  = this.childNodes;
  13.     if (0 == cn.length) {
  14.         return str + "/>";
  15.     } else {
  16.         str += ">";
  17.     }
  18.     for (var l, i=0, l=cn.length; i<l; i++) {
  19.         if (3 == cn[i].nodeType || 4 == cn[i].nodeType) {
  20.             if (String.trim) {
  21.                 str += cn[i].data.trim();
  22.             } else {
  23.                 str += cn[i].data;
  24.             }
  25.         }
  26.         else if (1 == cn[i].nodeType) {
  27.             str += cn[i].getString();
  28.         }
  29.     }
  30.     return str + "</" + tag + ">";
  31. }
Nov 24 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.