通過 JavaScript 在標題或 HTML 中動態包含腳本元素 (Dynamically include script element in header or HTML by JavaScript)


問題描述

通過 JavaScript 在標題或 HTML 中動態包含腳本元素 (Dynamically include script element in header or HTML by JavaScript)

I have a script to the third party JavaScript library which requires token to be injected dynamically. 

I was told to include script below into the head of my html. I need to replace {your_token} in order for this to work. However this token is only available at runtime so I need to inject this script into the header of my application at runtime (init for the application code) 

  

<script>(function(e,b){if(!b.__SV){var a,f,i,g;window.mixpanel={your_token}...

What is the best way to replace your_token with right token and inject this script at the header of the application? 


參考解法

方法 1:

Just as Casey asked, it depends on where this script block come from?

  1. Backend replacement

    If you use any backend template engine, you probably have the opportunity to replace that token into the one you have in hand. So the output will be the script contains the real token the script needed.

  2. Front end replacement

    If you are using some template in front such as using handlebar template(other template engines works almost the same) <script type="text/x‑handlebars‑template">your third party js code <token here></script>, you can replacing the token when generating the dom element.

For me I prefer do this at backend.

方法 2:

Assuming that "this token is only available at runtime" means "from the onModuleLoad" (for whichever reason), then you could have the script as a String in your GWT app (you might want to use a ClientBundle with the script in a TextResource, or simply a String constant in your code), replace {your_token} with String#replace and then inject the resulting script with the ScriptInjector.fromString.

Though if you can, use "backend replacement" as proposed by Mike Li with a dynamic host page.

(by user_1357Mike LiThomas Broyer)

參考文件

  1. Dynamically include script element in header or HTML by JavaScript (CC BY‑SA 3.0/4.0)

#javascript #gwt






相關問題

為什麼我不能在 IE8 (javascript) 上擴展 localStorage? (Why can't I extend localStorage on IE8 (javascript)?)

在 Javascript 中打開外部 sqlite3 數據庫 (Open external sqlite3 database in Javascript)

Javascript:數組中的所有對像都具有相同的屬性 (Javascript: All Objects in Array Have Same Properties)

為什麼我們要在 javascripts 原型中添加函數? (Why do we add functions to javascripts prototype?)

顯示 URL javascript 的最後一部分? (Display the last part of URL javascript?)

Javascript XMLHttpRequest:忽略無效的 SSL 證書 (Javascript XMLHttpRequest: Ignore invalid SSL Certificate)

有沒有辦法測試 console.log 整體 (Is there a way to test for console.log entires)

如何從 javascript 對像中獲取名稱和值到新列表中? (How to get name and values from a javascript object into a new list?)

數據未發布..幫助!html,js,firebase (Data not posting.. Help! html,js,firebase)

使用 Node.js 腳本查看表單數據 (Seeing form data with Node.js script)

使用百分比查找範圍內的值 (find the value within a range using percent)

如何通過 react.js 中的組件傳遞變量或數據? (How to pass varible or data through components in react.js?)







留言討論