0

Basically, I've got a pretty simple setup here, I need to register an event listener, so I did that on init. This works really well in Salesforce classic. In lightning experience, the init method doesn't load. I first thought the event doesn't register, but soon I realized the problem is init method is never called. Here's the code:

<script>
        function init()
        {
            console.log("adding event listener");
            window.addEventListener("storage", handleMessage);
        }

        function handleMessage(e)
        {
            console.log("received");
            console.log(e);
            if(e.key === 'message')
            {
                window.script.clear();
                window.script.load(e.newValue);
            }
        }
    </script>
    <body onload="init()">
        <div id="outterId" style="position: absolute;">
            <div id="innerId" style="min-width: 100vw !important;min-height: 100vh !important;"></div>
            <script src="someSrc" id="someId" client-token="{!userToken}" embedded="true"></script>
        </div>
    </body>

Once again, this works fine in classic, but in lightning experience init doesn't load, it never logs anything into the console, but the script loads fine, and works as well as in classic.

Has anyone met this issue before? Does lightning experience have some sort of restrictions on inits of visualforce pages? How would I make it load the method/is it possible to do so?

1 Answer 1

1

Visualforce uses onload to load some stuff, so adding a handler here can cause problems. Instead:

<script>
  function init() ... omitted ...
  function handleMessage() ... omitted ...
  window.addEventListener('DOMContentLoaded',init);
</script>

This final line will let your init method execute as soon as the page finishes loading. Note that DOMContentLoaded is case sensitive, but you're allowed to use either this casing or all lowercase (but no other combination thereof).

1
  • Thank you, your solution worked! Commented May 6, 2020 at 14:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .