store source code in a variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TimSki
    New Member
    • Jan 2008
    • 83

    store source code in a variable

    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
  • limweizhong
    New Member
    • Dec 2006
    • 62

    #2
    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)

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      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
      Code:
      // though the intended purpose of this is somewhat specialized…
      Element.prototype.getString = function()
      {
      	var tag = this.tagName;
      	var str = "<" + tag;
      	var atb = this.attributes;
      	for (var l, i=0, l=atb.length; i<l; i++) {
      		if (i in atb) {
      			str += " " + atb[i].name + '="' + atb[i].value + '"';
      		}
      	}
      	var cn  = this.childNodes;
      	if (0 == cn.length) {
      		return str + "/>";
      	} else {
      		str += ">";
      	}
      	for (var l, i=0, l=cn.length; i<l; i++) {
      		if (3 == cn[i].nodeType || 4 == cn[i].nodeType) {
      			if (String.trim) {
      				str += cn[i].data.trim();
      			} else {
      				str += cn[i].data;
      			}
      		}
      		else if (1 == cn[i].nodeType) {
      			str += cn[i].getString();
      		}
      	}
      	return str + "</" + tag + ">";
      }

      Comment

      Working...