简体   繁体   中英

How to inject a script tag using webpack4

I´m trying to insert dynamically a script tag:

let div = document.createElement('div')
let scriptTag = document.createElement('script')
scriptTag.type = "text/javascript";
scriptTag.innerHTML = `
function closeModal() {
document.getElementById('contact-edit-modal${contact.id ? '-'+contact.id : ''}').remove()
}
function saveChanges('${createContact}'){
debugger
if ('${createContact}') {
  // creating new contact

  let userData = window.app.getUser()
  if (userData){
    let apiKey = userData.apiKey
    if (apiKey){
      let clientId = userData.clientId
      if (clientId) {
        gapi.client.init({... `

div.append(scriptTag) 
document.body.append(div)

and it is in fact injecting the script tag inside the DOM tree

在此处输入图片说明

but the debugger keyword is not making the interpreter stop

在此处输入图片说明

My webpack.config.js is in this link:

https://pastebin.com/x3GqrdWG

I am not 100% sure on this one but I think that debugger statements are parsed only at initial load meaning that it's not possible to add debugger statement dynamically.

BTW why are you adding tag in a div? Why not just append it on window.body?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM