dalawh Posted June 30, 2012 Share Posted June 30, 2012 I notice when I try to store html code in a variable and there is an ENTER between any code, the code does not work. Example: function displayRegister(){var a='<div></div>';} Code above is fine. function displayRegister(){var a='<div></div>';} Code above does not work. Is there a reason for this? Link to comment Share on other sites More sharing options...
eTianbun Posted June 30, 2012 Share Posted June 30, 2012 (edited) a='<div>\</div>'Add back-slash to the end of the line. Edited June 30, 2012 by CodeName Link to comment Share on other sites More sharing options...
dalawh Posted July 1, 2012 Author Share Posted July 1, 2012 a='<div>\</div>' Add back-slash to the end of the line.It works. Can you explain to me why this is happening? Link to comment Share on other sites More sharing options...
astralaaron Posted July 1, 2012 Share Posted July 1, 2012 when you go down to the next line it ads a "\n" the newline character and I think that javascript doesn't allow that in the middle of a string, the "\" is the escape character and escapes the \n character Link to comment Share on other sites More sharing options...
eTianbun Posted July 1, 2012 Share Posted July 1, 2012 (edited) It works. Can you explain to me why this is happening? Each statement/request passed to the browser, is seperated using semi-colon ( ; ), or by moving to the next line of the script (Its a way to denote one statement, from another). So when you write:a='<div> Lets say a variable is created, called 'a' and at the time the browser wants to create the string, that will be assigned to it, it finds out that you are missing the closing apostrophe, then it generate error. Edited July 1, 2012 by CodeName Link to comment Share on other sites More sharing options...
dalawh Posted July 1, 2012 Author Share Posted July 1, 2012 Each statement/request passed to the browser, is seperated using semi-colon ( ; ), or by moving to the next line of the script (Its a way to denote one statement, from another). So when you write:a='<div> Lets say a variable is created, called 'a' and at the time the browser wants to create the string, that will be assigned to it, it finds out that you are missing the closing apostrophe, then it generate error. I am not missing a semi colon at the end. Not really sure where you are getting at. Link to comment Share on other sites More sharing options...
Guest So Called Posted July 1, 2012 Share Posted July 1, 2012 (edited) I'm not very knowledgeable about JS but as far as I understand the problem of the OP is that an end of line is a valid terminating character in JS, so a statement like a='<div> on a line by itself is not valid since there's an open quote. But JS has a line continuation character \ that tells it to ignore the default end of line and continue evaluating on the next line. Compare that with PHP which in most ways is very much like C in that white space (including newline characters) are ignored. A quote (or other statement) in PHP will automatically continue evaluating on the next line until a semicolon is reached. I don't particularly like the line termination features in JS but one has to live with what is. You'll need to affix a \ to that two line quote to avoid the error. Edited July 1, 2012 by So Called Link to comment Share on other sites More sharing options...
dalawh Posted July 1, 2012 Author Share Posted July 1, 2012 I'm not very knowledgeable about JS but as far as I understand the problem an end of line is a valid terminating character in JS, so a statement like a='<div> on a line by itself is not valid since there's an open quote. But JS has a line continuation character \ that tells it to ignore the default end of line and continue evaluating on the next line. Compare that with PHP which in most ways is very much like C in that white space (including newline characters) are ignored. A quote in PHP will automatically continue evaluating on the next line until a semicolon is reached. I don't particularly like the line termination features in JS but one has to live with what is. You'll need to affix a \ to that two line quote to avoid the error.Thanks for explaining this. Makes much more sense. 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