Jump to content

Learn something new everyday.


Recommended Posts

So I have been programming with JavaScript for like oh gosh 18 some years now and am constantly looking for better methods.  Last night I stumbled upon something I had never seen before and was kinda interested if it would really work because Dreamweaver was super angry about it so i figured it was out of convention.

Anyways what I am referring to is literal strings... like you know how if you wanted to create a dynamic element so lets say a div or a textarea.

Normally I thought to do this you would always need to just chain a string together so something like this:

var block = document.createElement('div');

block.innerHTML = "Som stuff bllah elements"+
"New like of string stuff because who likes long strings"+
"by this point im annoyed...";

And that's is a pain like a super pain... and what happens when I want literals and dont want to          to make extra spaces and the such, like for real im sure you can all agree it sucks.

Anyways I discovered use that all time neglected ``  you know the Gave Accent that no one ever uses... yeah well that trick little bugger makes JS ignore all formatting and calculate it as a literal string as far as I can tell so

the inneHTML declaration ends up looking like this.
block.innerHTML =
Especially if trying to show formated scripts

The breaklines and tabs come through, and it ends up being way
more readable and editable.`;

like I know its very specific and I have not really tested it much... but I was like kinda stoked about it so I figured someone else might be too...

Link to comment
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.
Note: Your post will require moderator approval before it will be visible.

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.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...