Jump to content

ENTER in Javascript issues


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 post
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 post
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 post
Share on other sites
Guest So Called

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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...