簡體   English   中英

如何將整個html文件嵌入JS

[英]how to embed the whole html file into JS

我正在嘗試在我的3個網站上實現在線聊天功能,但是我不想將我的整個代碼復制粘貼到每個網站上。 我想創建類似tawk.to東西。 他們只有一個<script>標記,我們在頁面上粘貼了一個很小的代碼,整個功能通過JS呈現。

現在,我將其保存在HTML文件中,而我的邏輯在同一HTML文件中的JS中。

像這樣嗎

<script src="path-to-my-file.js"></script>

基本上,在我頁面的標簽中,我將包含此JS。

<!DOCTYPE HTML>
<body>
<script src="loads-html-from-other-place"></script>
</body>

這是tawk.to的工作:

<!--Start of Tawk.to Script-->
<script type="text/javascript">
var Tawk_API=Tawk_API||{}, Tawk_LoadStart=new Date();
(function(){
var         
s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0];
s1.async=true;
s1.src='https://embed.tawk.to/12341/default';
s1.charset='UTF-8';
s1.setAttribute('crossorigin','*');
s0.parentNode.insertBefore(s1,s0);
})();
</script>
<!--End of Tawk.to Script-->

您只需將此代碼粘貼到頁面中,它將顯示聊天框以及功能

是的,您可以按照以下方法進行操作。

....
</body>
<script src="path-to-my-file.js"></script>

創建一個JS文件

IN that file write a logic to create chat button.
That file can have click events of that button which is added at run time VIA ajax.
You can then include this file in all the HTML pages where you need chat functionality.
Make sure you load this file at the end of body tag.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM