If you're using jQuery you could just listen for the window load event and then call the open method for your Magnific Popup like so:
(function($) {
$(window).load(function () {
// retrieved this line of code from http://dimsemenov.com/plugins/magnific-popup/documentation.html#api
$.magnificPopup.open({
items: {
src: 'someimage.jpg'
},
type: 'image'
// You may add options here, they're exactly the same as for $.fn.magnificPopup call
// Note that some settings that rely on click event (like disableOn or midClick) will not work here
}, 0);
});
})(jQuery);
Answer from bbone on Stack OverflowIf you're using jQuery you could just listen for the window load event and then call the open method for your Magnific Popup like so:
(function($) {
$(window).load(function () {
// retrieved this line of code from http://dimsemenov.com/plugins/magnific-popup/documentation.html#api
$.magnificPopup.open({
items: {
src: 'someimage.jpg'
},
type: 'image'
// You may add options here, they're exactly the same as for $.fn.magnificPopup call
// Note that some settings that rely on click event (like disableOn or midClick) will not work here
}, 0);
});
})(jQuery);
I was able to get a timed modal working using jquery's setTimeout function, Just wrap .magificpopup in the settimeout function to set a delay. Change the value of 5000 (5 seconds) to whatever value you want.
See below:
$(document).ready(function () {
setTimeout(function() {
if ($('#myModal').length) {
$.magnificPopup.open({
items: {
src: '#myModal'
},
type: 'inline'
});
}
}, 5000);
});
Magnific Popup on Page Load - javascript
magento 2.1 - How to load a popup in the home page? - Magento Stack Exchange
how to load magnific popup modal on page load
Load magnific popup on page load - javascript
$.magnificPopup.open({
items: {
src: '#test-popup',
type: 'inline'
}
});
If you can give link to check the problem it will be good enough.
Try this code below
<script>
function getPathFromUrl(url) {
return url.split(/[?#]/)[0];
}
if(getPathFromUrl(document.referrer) != getPathFromUrl(document.URL){
$.magnificPopup.open({
items: {
src: '#test-popup',
type: 'inline'
}
});
}
</script>
Below is solution for time based popup
var now, time, lastTimePopup, repeatAfter;
now = new Date();
time = now.getTime();
repeatAfter = 2 * 60 * 1000;//First number 2 is after number of minutes the popup should be showed.
if (localStorage.getItem('lastTimePopup') !== null) {
lastTimePopup = localStorage.getItem('lastTimePopup');
}
if (((now - lastTimePopup) >= repeatAfter) || !lastTimePopup) {
$.magnificPopup.open({
items: { src: '#test-popup' },
type: 'inline'
}, 0);
localStorage.setItem('lastTimePopup', now.getTime());
}
In the documentation I found this:
// Open directly via API
$.magnificPopup.open({
items: {
src: '<div class="white-popup">Dynamically created popup</div>', // can be a HTML string, jQuery object, or CSS selector
type: 'inline'
}
});
You should wrap this in a $(document).ready(function() {...});
I have created a working Plunker sample for you: https://plnkr.co/edit/h1fmGbJg5cYONfwMQerF
Another solution might be to add a hidden element (e.g. a button) and click it via Javascript: $('.thebutton')[0].click();
I used magnific popup you can download source from Link
VendorName: Prince
ModuleName: Popup
You can see my full Popup module Here: Github
app/code/Prince/Popup/view/frontend/layout/cms_index_index.xml
<?xml version="1.0" ?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<link src="Prince_Popup/js/jquery.magnific-popup.min.js"/>
<css src="Prince_Popup/css/magnific-popup.css"/>
<css src="Prince_Popup/css/magnific-animation.css"/>
</head>
<body>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="index.home" template="Prince_Popup::index/popup.phtml"/>
</referenceContainer>
</body>
</page>
app/code/Prince/Popup/view/frontend/requirejs-config.js
var config = {
map: {
'*': {
magnificPopup: 'Prince_Popup/js/jquery.magnific-popup.min',
popup: 'Prince_Popup/js/prince_popup'
}
},
shim: {
magnificPopup: {
deps: ['jquery']
}
}
};
app/code/Prince/Popup/view/frontend/templates/index/popup.phtml
<div id="popup-content" class="popup-content mfp-with-anim" style="display:none">
YOUR CONTENT HERE
</div>
<script type="text/javascript">
requirejs(['jquery', 'popup' ],
function ($, popup) {
$(document).ready(function(){
$.magnificPopup.open({
items: {
src: '#popup-content'
},
type: 'inline',
removalDelay: 500
});
$('#popup-content').css('display','inline block');
});
});
</script>
My upper answer is for magnify popup but if you want default magento 2 modal popup on homepage you need to create this files
app/design/frontend/Vendor/theme/Magento_Cms/layout/cms_index_index.xml
<?xml version="1.0" ?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<link src="Magento_Cms/js/popup.js"/>
</head>
<body>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\template" name="index.home" template="Magento_Cms::popup.phtml"/>
</referenceContainer>
</body>
</page>
app/design/frontend/Vendor/theme/Magento_Cms/templates/popup.phtml
<div id="popup-content" class="popup-content" style="display:none">
YOUR CONTENT HERE
</div>
app/design/frontend/Vendor/theme/Magento_Cms/web/js/popup.js
require(
[ 'jquery', 'Magento_Ui/js/modal/modal' ],
function($, modal) {
modal({
autoOpen: true,
responsive: true,
clickableOverlay: false,
modalClass: 'modal-custom',
title: 'Popup'
}, $("#popup-content"));
}
);
Try this:
$.magnificPopup.open({
items: {
src: '<div id="test-modal" class="white-popup"><h1>Modal Test</h1><p>Test Some text.</p><p><a class="popup-modal-dismiss">Dismiss</a></p></div>',
type:'inline'
},
modal: true
});
And the fiddle...
$.magnificPopup.open({items: {src: '#id-of-your-popup'},type: 'inline'}, 0);
What you're doing is you're binding a click event on #popup-with-zoom-anim that's why it's not triggering on load. You must call the magnificpopup open method to open it onload.
Try this:
$(document).ready(function() {
$.magnificPopup.open({
items:{
src: '#small-dialog',
type: 'inline'
},
fixedContentPos: true,
fixedBgPos: true,
overflowY: 'auto',
closeBtnInside: true,
preloader: false,
midClick: true,
removalDelay: 300,
mainClass: 'my-mfp-zoom-in'
});
});
If you visit the documentation http://dimsemenov.com/plugins/magnific-popup/documentation.html#initializing-popup you can see that you have an open option:
$.magnificPopup.open({
items: {
src: 'some-image.jpg'
},
type: 'image'
});
Since what you want is to open html that is located in your html, you should set type as inline. Example (with button):
// From an element with ID #popup
$('button').magnificPopup({
items: {
src: '#popup',
type: 'inline'
}
});
So finally you should have something like this:
<div>
<a class="popup-with-zoom-anim" href="#small-dialog" >Open with fade-zoom animation</a><br/>
<div id="small-dialog" class="zoom-anim-dialog mfp-hide">
-- some content ---
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$.magnificPopup.open({
src: '#small-dialog',
type: 'inline'
});
});
</script>
I have not tested it but I hope it can helps.
Cheers
$('.external-media').click(function( event ) {
event.preventDefault();
var items = [];
$.ajax({
url: jQuery(this).attr('href'),
type:'GET',
success: function(data){
$(data).find('.slide').each(function() {
items.push({
src: $(this).attr("src"),
title: $(this).attr("alt")
});
});
},
complete: function() {
$.magnificPopup.open({
items: items,
type: 'image',
gallery: { enabled: true }
});
}
});
});
I don't see items: in your script.
$('.open-gallery-link').click(function() {
/*an items array that will get passed to the popup*/
var items = [];
$( $(this).attr('href') ).find('.slide').each(function() {
items.push( {
src: $(this)
} );
});
$.magnificPopup.open({
items:items,
gallery: {
enabled: true
}
});
});
For multiple instances:
$('.first-popup-link, .second-popup-link').magnificPopup({
closeBtnInside:true
});
many more examples are here
