Jump to content

ENTER in Javascript issues


dalawh
 Share

Recommended Posts

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

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 by CodeName
Link to comment
Share on other sites

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

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 by So Called
Link to comment
Share on other sites

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

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