Jump to content

html/Javascript code which loads itself again


Rachit Shah
 Share

Recommended Posts

I have been trying to generate same page through javascript.I mean the script function will again use document.write to write the same code as it is in the original html page.Please Put this code in HTML page.Click the button once provided in html page.Click again.______________________The problem starts now. Now click the button again. Its behaviour is different now.It will double the document code (duplication of code through script).Can anybody justify the behaviour.Thanx in advance.Plz put the code in html page and check it, only then one can actually understand the strange behaviour. I cant put it in words.

<html><head><script id='script1' name='script1' type="text/javascript">function writeIt () { var x=""; x = x + "<html>\n<head>\n"; x= x + "<script>" + window.document.scripts(0).text; x = x + "</script" + ">\n"; x = x + "</head>\n<body>\n"; x = x + document.body.innerHTML; x = x + "\n</body>\n</html>"; alert(x); document.write(x);}</script></head><body><input type="button" onclick="writeIt()" value="Write"/>rachit</body></html>
Link to comment
Share on other sites

I tried with document.close and document.openBoth are working and I have solved the problem.

<html><head><script id='script1' name='script1' type="text/javascript">function writeIt () {var x="";x = x + "<html>\n<head>\n";x= x + "<script>" + window.document.scripts(0).text;x = x + "</script" + ">\n";x = x + "</head>\n<body>\n";x = x + document.body.innerHTML;x = x + "\n</body>\n</html>";alert(x);document.close(); // or document.open();document.write(x);}</script></head><body><input type="button" onclick="writeIt()" value="Write"/>rachit</body></html>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...