Rachit Shah Posted March 21, 2006 Share Posted March 21, 2006 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 More sharing options...
Webworldx Posted March 21, 2006 Share Posted March 21, 2006 Basically because it reads the </script> and terminates the code. If you did <\/script> it'll read it fine Link to comment Share on other sites More sharing options...
Rachit Shah Posted March 22, 2006 Author Share Posted March 22, 2006 Basically because it reads the </script> and terminates the code. If you did <\/script> it'll read it fine <{POST_SNAPBACK}> 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 More sharing options...
snowboard01 Posted March 22, 2006 Share Posted March 22, 2006 try control f5 sometimes my pages do that booting from cache and then they always look bad Link to comment Share on other sites More sharing options...
Webworldx Posted March 22, 2006 Share Posted March 22, 2006 <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 More sharing options...
Rachit Shah Posted March 23, 2006 Author Share Posted March 23, 2006 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 More sharing options...
Rachit Shah Posted March 23, 2006 Author Share Posted March 23, 2006 try control f5 sometimes my pages do that booting from cache and then they always look bad<{POST_SNAPBACK}> Plz first understand my question.. I have put it as new thread. Check it, I may need your help. Link to comment Share on other sites More sharing options...
Chocolate570 Posted March 23, 2006 Share Posted March 23, 2006 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 More sharing options...
Rachit Shah Posted March 23, 2006 Author Share Posted March 23, 2006 Chocolate plz check this. I think you need to understand the problem.http://w3schools.invisionzone.com/index.php?showtopic=3328What u have suggested is not related to the understanding of the behaviour of browser.Plz read the question carefully. cuz I may need help from you. Check the link above. Link to comment Share on other sites More sharing options...
Chocolate570 Posted March 24, 2006 Share Posted March 24, 2006 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 More sharing options...
Rachit Shah Posted March 24, 2006 Author Share Posted March 24, 2006 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now