简体   繁体   中英

Could not include javascript file dynamically using angular4 and Javascript

I am unable to include one javascript file dynamically inside angular4 submit method. I am explaining my code below.

onTextFormSubmit(){
    this.processValidation = true;
    if (this.textForm.invalid) {
      return;
    } 
    let urlValue = this.textForm.value;
    let url = urlValue.url;
      console.log('hello',url);
      var script = document.createElement("SCRIPT");
      script.setAttribute("type", "text/javascript");
      script.setAttribute("src", url);
      script.setAttribute('integrity', 'sha384-tEgmOghGBnPJQ3h63qzTE8V69SDVkIIyQeOFDfsG+AWJqA/UfZMmyQcXQVunl3wT');
      script.setAttribute('crossorigin','anonymous');
      document.getElementsByTagName("head")[0].appendChild(script);
  }

Here the url parameter is coming from form submit which is like https://code.jquery.com/jquery-2.2.4.min.js this and I am including it dynamically but after submit when I am doing view page source and searching that file isnide the head tag, I did not found there which is my issue. I need to include this file dynamically.

Iv'e tested it in my environment and it also failed. Iv'e got the following error:

Failed to find a valid digest in the 'integrity' attribute for resource ' https://code.jquery.com/jquery-2.2.4.min.js ' with computed SHA-256 integrity 'BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44='. The resource has been blocked.

After a little search I found this git issue that resolved it.

Simply add the following attribute to your script:

 script.setAttribute('integrity','');

Edit:

If it's still doesn't work try this :

script.setAttribute('integrity','sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=');
script.setAttribute('crossorigin','anonymous');

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