0

I have the following in my html

<html>
<body>
<script type="text/javascript">
 var hccid=98964571;
 function add_chatinline(){
    var nt=document.createElement("script");
    nt.async=true;
    nt.src="http://localhost/ll.js";
    var ct=document.getElementsByTagName("script")[0];
    ct.parentNode.insertBefore(nt,ct);
    console.log("state is ", SORCHAT)//SORCHAT is not defined
 }
 add_chatinline();
 </script>
</body>
</html>

On the ll.js i have

var SORCHAT = SORCHAT || (function () {
return {
    init: function (Args) {
        console.log("hash is ", Args)
    },
};
}());

But now am getting an error of SORCHAT is not defined.

By adding window.onload that is

 <script>
  window.onload = function(){
    SORCHAT.init(12736474676); //this works
 } 
 </script>

But whenever i include another javascript file with window.onload function the SORCHAT.init is not executed.

What am i missing.

1

1 Answer 1

2

You are probably overwriting the window.onload when using it multiple times. You can prevent that with the help of the addEventListener-function.

window.onload = function () {
  console.log('onload #1');
}
window.onload = function () { // This replaces the first onload (#1)
  console.log('onload #2');
}

window.addEventListener('load', function () {
  console.log('onload #A');
});

window.addEventListener('load', function () {
  console.log('onload #B');
});

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.