Skip to content

Magento 2 How to load Js first before the page load

I have created a custom module in which I created a popup modal in custom location.js with require js on and add this js in default.xml file.

On the first page load popup is open after the page load i want to load this js before the page load.

Here’s the default.xml wihich I override

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
  <link src="Vendor_Module::js/location.js"/>
</head>
<body>
    <referenceContainer name="page.wrapper">
        <block class="MagentoFrameworkViewElementTemplate" name="custom-block-name" template="Vendor_Module::popup.phtml" ifconfig="nearbyproducts/general/enable"/>
    </referenceContainer>
</body>

Can you please suggest how can I load js before page load or stop the page load?