0

I'm tryng to put html into javascript code:

       document.write("<a class=\"twitter-timeline\"href=\"https://twitter.com/test\"          data-widget-    id=\"248798161076883457\">Tweets by @test</a>
             <script>!function(d,s,id){
              var js,fjs=d.getElementsByTagName(s)[0];
             if(!d.getElementById(id))
              {js=d.createElement(s);
          js.id=id;js.src=\"//platform.twitter.com/widgets.js\";
              fjs.parentNode.insertBefore(js,fjs);
        }
       }(document,\"script\",\"twitter-wjs\");
       </script>
        "); 

}

The error message is: Unexpected token ILLEGAL what's wrong? Thx!

6
  • change //platform.twitter.com to either http://platform.twitter.com or platform.twitter.com Commented Sep 20, 2012 at 15:28
  • @karthikr: No need to do that, that's a valid protocol-relative URL. Commented Sep 20, 2012 at 15:30
  • document.write is a bad function to use for several reasons including security issues. Consider instead using an element's innerHTML property to set HTML into your document, which works better and does not have the same security issues. Commented Sep 20, 2012 at 15:30
  • document.write("<a class='twitter-timeline' href='twitter.com/test' data-widget- id='248798161076883457'>Tweets by @test</a><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src='platform.twitter.com/widgets.js'; fjs.parentNode.insertBefore(js,fjs);}}(document,'script','twitter-wjs');</script>"); ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ Commented Sep 20, 2012 at 15:30
  • Glad it works! Which script you used in the end? the 1 that I shared or the 1 shared by user1666993 . Thanks Commented Sep 20, 2012 at 15:38

2 Answers 2

4

A string in javascript must not go over multiple lines without the right format:

   document.write("<a class=\"twitter-timeline\"href=\"https://twitter.com  /test\"          data-widget-    id=\"248798161076883457\">Tweets by @test</a>\
         <script>!function(d,s,id){\
          var js,fjs=d.getElementsByTagName(s)[0];\
         if(!d.getElementById(id))\
          {js=d.createElement(s);\
      js.id=id;js.src=\"//platform.twitter.com/widgets.js\";\
          fjs.parentNode.insertBefore(js,fjs);\
    }\
   }(document,\"script\",\"twitter-wjs\");\
   </script>\
    "); 

In general:

string = "Multiple\
Lines\
supported."
Sign up to request clarification or add additional context in comments.

Comments

0
document.write("<a class='twitter-timeline' href='https://twitter.com/test'  data-widget- id='248798161076883457'>Tweets by @test</a><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src='platform.twitter.com/widgets.js'; fjs.parentNode.insertBefore(js,fjs);}}(document,'script','twitter-wjs');</script>"); ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

You can use single quotes instead of double quotes inside script! It makes it easier to read and it works perfectly

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.