javascript –  Feb 15 '15 at 21:09

  • Updated the answer to address your comment. –  Feb 15 '15 at 21:14
  • i still have to remove my async to make this work. otherwise it adds it to the bottom. :-( may be because of async it finishes processing last and so it adds to the last of the page? –  Feb 15 '15 at 21:20
  • @SatyaShot If you want this to append at a particular point - is there an element you can use to place it adjacent to or inside? Or is it literally going within the body element and has no sibling elements? – pwdst Feb 15 '15 at 21:26
  • this is like an sort of a widget, any body can insert this piece of code anywhere they want. this is the widget := –  Feb 15 '15 at 21:27
  • I have seen many widgets like google analytics or google ads. I am not sure how they exactly place. may be there must be a logic which I am trying to decode but stuck! –  Feb 15 '15 at 21:30
  • @SatyaShot I'd suggest adding an ID to the script element that calls your live trend script file then - and inserting the created element **before or after that** as per bobdye's edit. Use of document.write is bad practice for a number of reasons - and completely incompatible with the async attribute - for reasons that are listed at http://javascript.crockford.com/script.html – pwdst Feb 15 '15 at 21:32
  • I can always add a
    to the widget and i can set the innerhtml of the someid to what i want. I know this will work but can this be acheiveed by just the script tags alone???
    –  Feb 15 '15 at 21:32
  • I added an id to script tag and this works.This is acheived by having two script tags. the first script tag with the id and the next script tag is actually the widget. am i rite? –  Feb 15 '15 at 21:37
  • thanks bobdye. your answers certainly helped me a lot! –  Feb 15 '15 at 21:54
  • @SatyaShot I have taken the liberty of tweaking bobdye's answer to remove the need for the second script element, and to insert the required HTML at the point of the remaining script element. It should also work with the async attribute now. – pwdst Feb 15 '15 at 22:22
  • @pwdst Now i get a strange problem where if i put the widget twice it gives me only for once as the id becomes the same. I have put in another post for this problem [link]http://stackoverflow.com/questions/28619477/javascript-widget-async-two-widgets-gets-mixed –  Feb 20 '15 at 00:33
  • Here as per your code i want the id itself to be generic and passed to .js file. is that possible? –  Feb 20 '15 at 00:35