-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to insert <script> tag before </body>? #2000
Comments
can edit in the template html |
app.html?all pages will be inserted |
All |
emmm..i want to import 3rd party lib ,use script tag to import instead import . |
I guess you could include those scripts in the pages themselves. |
<template>
<div></div>
</template>
<script>
export default {
created() {
if (process.browser) {
let recaptchaScript = document.createElement('script')
recaptchaScript.setAttribute('src', 'https://www.google.com/recaptcha/api.js')
document.body.appendChild(recaptchaScript)
}
}
}
</script> |
@clarkdo I had tried this way ,but there are some reasons cant use this way. |
I guess you want to add scripts to the bottom to defer execution because the script is used on the page but not required for viewing the page? Actually I have asked for the same functionality in #1370, it would be nice it you could do this easily with nuxt. Although you can set the defer tag with vue-meta, which should defer execution, I still also prefer to put them psychically at the bottom of the page. |
Nuxt will generate a |
works ,same as <div>
<video id="azuremediaplayer" ></video>
<script src="azure/azuremediaplayer.min.js"></script>
</div> not in front of the </ body> tag |
There is no
It will have the same effect, even same as Even in |
Putting This strategy is mostly used for scripts that are not important to the functioning of the page itself, think of analytic scripts, delayed/async image loading and especially 3rd party ads. Although the browser caches the files probably themselves, you often want to make fully sure execution is delayed as you dont want your page to feel sluggish due to a heavy ad. Defer should do this as well, but putting it at the end of the body just physically makes sure it happens like this. |
if the script in static folder,will get an error=>cannot resolve xx.js in 'pages/xxx' |
@cdoneshot if it's in |
Hi, this is my way. <template>
<div>
<a class="twitter-timeline" href="https://twitter.com/nuxt_js">Tweets by nuxt_js</a>
<span v-if="twitterWidget">
<script src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
</span>
</div>
</template> <script>
export default {
data () {
return {
twitterWidget: false,
}
},
async mounted () {
this.twitterWidget = true
},
}
</script> |
@cdoneshot @pimlie {
metaInfo: {
bodyScript: [
{ src: 'azure/azuremediaplayer.min.js' }
]
}
} This feature is planned to 1.0.0 release for now. |
I came across this issue and still had to look for quite a while to find the correct syntax that was released in 1.0.0, so for reference:
src: this example from the 1.0.0 release notes |
Close, but we're not 100% there yet. Utilizing the method quoted by @phivk , the script will be put near the end of the < head > tag, not at the end of the < body >. While the defer property will allow parallel script fetch and downloads, the load ordering of js during the parsing phase is still off. Currently any deferred script will parse and execute before the Vuejs application script. Suppose you have a ton of scripts that can use async property, or scripts that dont need to execute before the application executes. We're forced to lose a chunk of time waiting. |
@ameno-gdrive why put scripts in html at all? Can't you just asynchronously import them in your javscript where you need them? |
@qm3ster |
Its very easy Just create a app.html at the root of your project and add
THats the default template that nuxtjs uses by default. Ensure that you place this at the root of your project |
@Geowan That will put the script on every page. @cdoneshot wanted it on specific pages |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
i want to insert some <script> tag before the </ body> tag in some pages. if i modify app.html ,all pages will be inserted,or i use head() in these pages,it will be inserted in the head,these are not my wanted
The text was updated successfully, but these errors were encountered: