Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor.js: <script> tag doesn't work inside <body>

A simple script tag inside the body tag doesn't seem to work. The alert doesn't get triggered in the code below:

<body>
   <script type="text/javascript">
      alert('Hello');
   </script>

      {{>main}}

</body>

Any idea why?

Edit: Just tried it with a fresh meteor app, no alert tag still:

<head>
  <title>test</title>
</head>

<body>

  <script type="text/javascript">
     alert('Hello');
  </script>

  {{> hello}}
</body>

<template name="hello">
  <h1>Hello World!</h1>
  {{greeting}}
  <input type="button" value="Click" />
</template>

Weird thing is when I copy paste the source of the html, made a new html page, and the alert will work.

Edit3: I deployed this app here: http://alert-in-body-test.meteor.com/ Do you get an alert box?

like image 468
Ricky Gu Avatar asked Aug 28 '12 08:08

Ricky Gu


2 Answers

This question is still relevant in the current version of Meteor (version 0.5.4) so I wanted to describe how to include script at the end of the body.

To execute javascript at the end of the body, register a Handlebars helper and put the relevant code there, like this:

In client.html:

<body>
  {{renderPage}}

  {{afterBody}}
</body>

...

In client.js:

if (typeof Handlebars !== 'undefined') {
  Handlebars.registerHelper('afterBody', function(name, options) {
    $('body').append('AFTER BODY');
  });
}

(For a great description of why this is required, see Rahul's answer to a similar question here: https://stackoverflow.com/a/14002991/219238 )

like image 117
alanning Avatar answered Oct 20 '22 04:10

alanning


Its working for me

in onrender call this jquery

$.getScript('yours url')

like image 41
mad Man Avatar answered Oct 20 '22 04:10

mad Man