简体   繁体   中英

how to embed the whole html file into JS

I am trying to implement an online chat functionality on 3 of my websites, but I dont want to copy paste my whole code on every website. I want to create something like tawk.to has. They just have a <script> tag and we paste a tiny code on our page and the whole functionality is rendered through JS.

Right now I have it in an HTML file, where my logic is in JS in the same HTML file.

Something like this?

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

Basically in my page's tag I will include this JS.

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

Here is what tawk.to does:

<!--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-->

You just paste this code in your page and it displays chat box along with the functionality

Yes you can do this by below approach.

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

Create a JS file

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.

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