最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Meteor.js: <script> tag doesn't work inside <body> - Stack Overflow

programmeradmin0浏览0评论

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: / Do you get an alert box?

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./ Do you get an alert box?

Share Improve this question edited Mar 6, 2014 at 7:11 Dan Dascalescu 152k65 gold badges333 silver badges420 bronze badges asked Aug 28, 2012 at 8:05 Ricky GuRicky Gu 5851 gold badge4 silver badges13 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

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./a/14002991/219238 )

Its working for me

in onrender call this jquery

$.getScript('yours url')

It should work.

I have just pasted this inside one of my projects and it worked.

Your {{>main}} is strange for me tough. Also make sure that <body> is inside <html> tag.

Meteor is constructing the entire DOM from Javascript by rendering your page as a template -- the 'source' for your page as seen by the browser is basically:

 <script type="text/javascript" src="/5a8a37bef5095c69bcd3844caf3532e1ba6d49bf.js"></script>

I can't find a definitive page stating that embedding a script tag in a template like this won't cause it to be executed, but it definitely feels against the spirit of what the framework is trying to achieve.

If the point is to achieve a clean separation of your markup and logic then why put it in the template. A better solution would be to use the meteor.startup call

Instead of looking the rendered html at developer tools, try looking the real downloaded html. You will find a (probably) empty tag, with tons of script tags inside the .

In other words, the body of your meteor application is not the body of the final html, it's just your main template.

Instead, this ton on scripts shipped by Meteor, will load your templates. So, your code will not run, cause it's been placed there. It's like when you manipulate DOM (with jQuery, for exemple), placing a script tag in DOM, after it's loaded. This script tag will not run.

发布评论

评论列表(0)

  1. 暂无评论