Jump to content

Here is interesting puzzle


Rachit Shah
 Share

Recommended Posts

I have been trapped in a puzzle. I want to retrieve the script code from the same html page and put it inside script tags. I have put button function which fetches the script in the page.While fetching script code I encountered below mentioned scenario. Here in the Original code its giving me error as unterminated string constant.I changed script as shown in second quoted code. Now its working. I did not figure out why its giving error in first case (i.e first code).Original code

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

Basically because it reads the </script> and terminates the code.  If you did <\/script> it'll read it fine :)

I think thats not true. Check the code below. Its working fine. The </head> is there, the browser iss not reading it?
<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

<html><head><script id='script1' name='script1' type="text/javascript">function writeIt () {var x="";x += "<html>\n<head>\n";x += "<script>" + window.document.scripts(0).text;x +=  "<\/script>\n";x += "<\/head>\n<body>\n";x += document.body.innerHTML;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>

Loads fine for me. I backslashed the head, body and html and tidied, although they weren't neccessary changes.

Link to comment
Share on other sites

If I change <\/script> to </script> its giving error.If I change <\/head> to </head> its not giving error. Why? Why? Why?Here is the working code. Its working fine. The above scenario is visible here. I have put it in red.

<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

Hah! Got it. Try this. And I modified Blue's script, by the way.

<html><head><script id="script1" name="script1" type="text/javascript">function writeIt (){var x="";y=document.getElementsByTagName("script");x += "<html>\n<head>\n";x += "<script>" + y[0].innerHTMLx +=  "<\/script>\n";x += "<\/head>\n<body>\n";x += document.body.innerHTML;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

Ahh, I think I understand now.Blue, on that topic, is correct. The moment the javascript rights </script>, the script is terminated, because the javascript thinks it's been terminated. The browser interprates it as a dynamic change of the length of the script, and closes the function/whatever the script is doing at the moment. That's your problem.

Link to comment
Share on other sites

Thanx for all that you have discussed here in this thread. It cleared a cruicial understanding of how javascript may parse the script.HTML/Javascript is like my hobby/interest.If I come to know something offbeat (advanced technical details) about javascript/HTML then I think I would accumulate advanced knowledge.I would say I have intermediate level understanding in HTML/Javascript. I want to be advanced in this topic and that was the reason I joined this forum. Dont take literal meaning of "offbeat". I dont mean unusual, I mean advanced or tricky.

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...