Such as Baidu Statistics code,we usually add a piece of pure js code to index.html :
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement(“script”);
hm.src = “//hm.baidu.com/hm.js?XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”;
var s = document.getElementsByTagName(“script”)[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
How can I add those code to Vuepress ?
Vuepress 0.14.2
I try to add those code to config.js:
module.exports = {
head: [ ['script', { ... }] ]
}
But I don't know how to do it.
Then I use a third part library vue-ba
install
yarn add vue-ba
then I modified enhanceApp.js :
import ba from 'vue-ba'
export default ({
Vue, options, router, siteData
}) => {
Vue.use(ba, 'xxxxxx')
}
when compiling,some error were outputed in console:
[10:47:08 AM] Compiled Server in 41s
[10:47:10 AM] Compiled Client in 43s
FAIL WAIT Rendering static HTML...
Rendering page: /infoError rendering /:
Visit ReferenceError: window is not defined
at Object.<anonymous> (/Users/DYT/Desktop/www.xiaoyulive.top/node_modules/vue-ba/dist/index.js:1:4368)
at n (/Users/DYT/Desktop/www.xiaoyulive.top/node_modules/vue-ba/dist/index.js:1:408)
at /Users/DYT/Desktop/www.xiaoyulive.top/node_modules/vue-ba/dist/index.js:1:791
at /Users/DYT/Desktop/www.xiaoyulive.top/node_modules/vue-ba/dist/index.js:1:801
at /Users/DYT/Desktop/www.xiaoyulive.top/node_modules/vue-ba/dist/index.js:1:146
at Object.<anonymous> (/Users/DYT/Desktop/www.xiaoyulive.top/node_modules/vue-ba/dist/index.js:1:285)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:20:18)https://yarnpkg.com/en/docs/cli/run
at r (/Users/DYT/Desktop/www.xiaoyulive.top/node_modules/vue-server-renderer/build.js:8346:16)
at Object.<anonymous> (webpack:/external "vue-ba":1:0)
at __webpack_require__ (webpack/bootstrap:25:0)
error Command failed with exit code 1.
error Command failed with exit code 1.
for documentation about this command.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Process finished with exit code 1
Expose window in enhanceApp.js, or a mechanism for adding pure js code.
Working with the config.js-approach:
module.exports = {
head: [ ['script', {}, `
var _hmt = _hmt || [];
(function() {
var hm = document.createElement(“script”);
hm.src = “//hm.baidu.com/hm.js?XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”;
var s = document.getElementsByTagName(“script”)[0];
s.parentNode.insertBefore(hm, s);
})();
`]]
}
This works, because of the head-option's signature:
['tagname', attributesObject, contentString]
Thanks, it works.
@ulivz Can we use similar technique to add it to the body?
would love to know how it works for adding script to body.
@ulivz How to add pure javascript code to the <body> in index.html?
@anisabboud See earlier comment, you need to do it in config.js
@rajaraodv config.js is a workaround that adds the script to the <head>.
I was wondering if there's a new way to add it to the <body> since @ulivz linked a long PR to this thread.
I was wondering if there's a new way to add it to the
<body>
@ulivz any updates here, please?
Most helpful comment
Working with the config.js-approach:
This works, because of the head-option's signature: