Skip to content
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 can I add pure js code to index.html #790

Closed
quanzaiyu opened this issue Sep 3, 2018 · 9 comments
Closed

How can I add pure js code to index.html #790

quanzaiyu opened this issue Sep 3, 2018 · 9 comments
Labels
has workaround Has a workaround type: enhancement Request to enhance an existing feature version: next Planned to do or already included in the next(1.0.0) version

Comments

@quanzaiyu
Copy link

quanzaiyu commented Sep 3, 2018

Bug report

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 ?

Version

Vuepress 0.14.2

Steps to reproduce

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

What is expected?

Expose window in enhanceApp.js, or a mechanism for adding pure js code.

What is actually happening?

Other relevant information

  • Your OS: macOS High Sierra 10.13.6
  • Node.js version: v10.6.0
  • Browser version: Chrome 68.0.3440.106
  • Is this a global or local install? local
  • Which package manager did you use for the install? yarn
@Tom-Julux
Copy link

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]

@ulivz ulivz added has workaround Has a workaround type: enhancement Request to enhance an existing feature labels Sep 4, 2018
@quanzaiyu
Copy link
Author

Thanks, it works.

@ulivz ulivz added version: next Planned to do or already included in the next(1.0.0) version resolved labels Sep 16, 2018
@rajaraodv
Copy link

@ulivz Can we use similar technique to add it to the body?

@yipcma
Copy link

yipcma commented Sep 23, 2018

would love to know how it works for adding script to body.

@anisabboud
Copy link

@ulivz How to add pure javascript code to the <body> in index.html?

@rajaraodv
Copy link

@anisabboud See earlier comment, you need to do it in config.js

@anisabboud
Copy link

@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.

@easeev
Copy link

easeev commented Dec 10, 2020

I was wondering if there's a new way to add it to the <body>

@ulivz any updates here, please?

@arthabus
Copy link

arthabus commented Jan 7, 2021

On possible approach to add script to body is using the same technique as @Tom-Julux showed:

module.exports = {
    head: [
        ['script', {id: "scriptImporter"}, `
        (function() { 
        var script = document.createElement("script"); 
        script.src = "path to your js script";
        setTimeout(() => document.body.append(script))
        })(); 
    `]
    ],

Note: since this code is running from head at which point body is not yet exist, we need to put the document.body.append inside setTimeout(fn. 0) so it waits one cycle before body is populated.

It's also possible to delete this script from the page if needed using the assigned id

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has workaround Has a workaround type: enhancement Request to enhance an existing feature version: next Planned to do or already included in the next(1.0.0) version
Projects
None yet
Development

No branches or pull requests

8 participants