Inline Modal Window w/ CSS and jQuery
Tags: Intermediate
There are many elegant and easy to install modal window (Popup) plugins out there, but there are times where the script interferes with some of the logic processed on the page. I recently had a situation where I was not able to use plugins like fancybox and prettyPhoto so I had to create my own modal window that pulled inline html within the page. Here is how I approached this situation:
HTML
We begin by adding a <a> tag with the following attributes:
href-#?w=500specifies the width of the popuprel– Set a unique rel for this Popupclass="poplight"– Class needed to trigger popup
<a href="#?w=500" rel="popup_name" class="poplight">Learn More</a>
Next we set up the inline markup for the popup. This can be placed anywhere on the page, I set this towards the bottom of the content. Notice that the id of the popup matches the <a> tag’s rel attribute. This is what associates the link and popup together.
<div id="popup_name" class="popup_block">
<h2>Turtle Power</h2>
<p>I <3 Turtles.</p>
</div>
CSS
Take a look at the comments below for an explanation of the styles. Notice that we don’t specify the margin on the .popup_block in the CSS. Since the popups may vary in size, we will have jQuery calculate this for us in the next step.
#fade { /*--Transparent background layer--*/
display: none; /*--hidden by default--*/
background: #000;
position: fixed; left: 0; top: 0;
width: 100%; height: 100%;
opacity: .80;
z-index: 9999;
}
.popup_block{
display: none; /*--hidden by default--*/
background: #fff;
padding: 20px;
border: 20px solid #ddd;
float: left;
font-size: 1.2em;
position: fixed;
top: 50%; left: 50%;
z-index: 99999;
/*--CSS3 Box Shadows--*/
-webkit-box-shadow: 0px 0px 20px #000;
-moz-box-shadow: 0px 0px 20px #000;
box-shadow: 0px 0px 20px #000;
/*--CSS3 Rounded Corners--*/
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}
img.btn_close {
float: right;
margin: -55px -55px 0 0;
}
/*--Making IE6 Understand Fixed Positioning--*/
*html #fade {
position: absolute;
}
*html .popup_block {
position: absolute;
}
Step 3. Setting up jQuery
For those who are not familiar with jQuery, do check out their site first and get an overview of how it works. I’ve shared a few tricks that I have picked up along the way, you can check those out as well.
Initial Step – Call the jQuery file
You can choose to download the file from the jQuery site, or you can use this one hosted on Google.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
Directly after the line where you called your jQuery, start a new <script> tag and start your code by using the $(document).ready event. This allows your jQuery code to run the instant the DOM is ready to be manipulated. The code you will be writing in the next few steps will all take place within.
$(document).ready(function() {
//Code goes here
});
Step 4. Activate Popup with jQuery
The following script contains comments explaining which jQuery actions are being performed.
//When you click on a link with class of poplight and the href starts with a # $('a.poplight[href^=#]').click(function() { var popID = $(this).attr('rel'); //Get Popup Name var popURL = $(this).attr('href'); //Get Popup href to define size //Pull Query & Variables from href URL var query= popURL.split('?'); var dim= query[1].split('&'); var popWidth = dim[0].split('=')[1]; //Gets the first query string value //Fade in the Popup and add close button $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>'); //Define margin for center alignment (vertical horizontal) - we add 80px to the height/width to accomodate for the padding and border width defined in the css var popMargTop = ($('#' + popID).height() + 80) / 2; var popMargLeft = ($('#' + popID).width() + 80) / 2; //Apply Margin to Popup $('#' + popID).css({ 'margin-top' : -popMargTop, 'margin-left' : -popMargLeft }); //Fade in Background $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag. $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies return false; }); //Close Popups and Fade Layer $('a.close, #fade').live('click', function() { //When clicking on the close or fade layer... $('#fade , .popup_block').fadeOut(function() { $('#fade, a.close').remove(); //fade them both out }); return false; });
References
- Getting the variable’s value from a link
- jQuery events not firing for dynamic content
- Explanation of
.live() - Explanation of
.prepend() - Explanation of
.append()
Alternate Usage
Final Thought
For those jQuery masters, please let me know if there are ways to improve this code. I am all open ears and hungry to learn :-)
For those who have questions or concerns, feel free to let me know! If you would like to use images or flash videos in the popups, please use the elegant plugin solutions since this code is only meant for a light weight inline popup.
Related Posts
Author Bio
My name is Soh Tanaka and I am a Los Angeles based designer/front-end developer specializing in CSS driven web design with an emphasis on usability and search engine optimization. I also run a CSS Gallery which is updated daily with the best CSS websites from around the world. Come check it out!
You can learn more about me or
Follow me on twitter for more updates and resources!
Did You Enjoy This Post?
Subscribe via RSS or by email to get all upcoming tutorials and articles delivered straight to you.





+ Add Comment149 Peeps Have Spoken Their Minds...
Thx for that tutorial. I’ve a question:
Is it possible to refresh a specific div on the parent site when closing the layer?
Best regards
Daniel
Hey Daniel, I don’t think I’m understanding your question, what do you mean by parent site?
cool soh! can this be use as a login pop-up too?
Hi Soh.
Everything’s working very well for me.
But I’ve a real question and that’s because I don’t find any solution that answers to it.
Maybe you will be able to answer me :
How can I manage to show a div content from an another page (like you could do with load jquery function) in a modalbox?
That could be with your plugin :
@AKI yes you can~
@Fred I wish I can help you but I really don’t know :-p The only method I know is pulling something on the direct page or referencing another file, iframe, or fetching data. I would ask that on http://stackoverflow.com to see if you can get an answer.
i’m hear in CSS3 we can make inline windows use pure CSS, but no smooth like this
hohoho…thanks for it MR.Sohtanaka
@Daniel – Yes you can, sort of!
Research the jQuery load() method. You can fetch data and place it into a specified HTML element without the need for a page refresh.
Remember: AJAX is your friend!
Hey Soh,
Always follow your tutorials. They are always brill, and this one is no exception. Will definitely be using it sometime, somewhere! I used your slider in a recent website: http://www.tkdbalbriggan.com (shamelss plug :-)
Thanks again
Hi very nice and lightweight plugin,
why not using “rel” attribute of href instead of “name” which is non exactly W3C standard-compliant?
@FRED
You mean getting the content something like this?
$(‘#result’).load(‘ajax/test.html #container’);
Pulling the data specifying some tag id. http://api.jquery.com/load/
@Sam & Amos – Thanks for helping out guys!
@WEBARTISAN great suggestion, I will be adjusting this tutorial now. Thanks!
@Ross your very welcome, I’m glad they came in handy :-)
@Amos
Hello Amos and thanks for your answer.
I know this function and how to use it. What I want to do is to do the same thing with the use of a modalbox like soh’s one.
How to do this?
@Fred
It depends on the escenario what do you have in mind? Trying to reach an external file is usually used to query a database. In that case i would use something like this:
//Here Soh gets the ID from the href
var popID = $(this).attr(‘rel’);
//Send the ID to the external file ( process.php )
$.ajax({
type:’POST’,
url: ‘process.php’,
data:’ID=’+popID ,
//On succes, set the result as the content of the modalbox
success: function(result){
$(‘#modalbox’).html(result);
}
});
//Here Soh sets the modalbox width and the close button
$(‘#modalbox’).css({ ‘width’: Number( popWidth ) }).prepend(‘‘);
Note that since it will be querying a database, the results vary, so there is only one element needed to display them ( #modalbox ). As you may now there are other methods that you can use instead of ‘$.ajax’ depending on your needs ( http://api.jquery.com/category/ajax/ ).
I hope i got your point this time.
Hi very simple and lightweight plugin.
Is there a simple way, to make this support jQuery UI?
@Sam Benson: thx for your reply. I’ll check that
@SOH: With parent I mean the opener window :)
Best regards,
Daniel
Excellent tutorial. Thanks for sharing. Ted
What about facybox? http://bitbonsai.com/facybox
Nice plugin Soh!
Any ideas on how to enable it to load once with the page?
I mean, to open a modal window when the page loads..
Hi Soh,
Thanks for your tut! I dont’ know jquery at all, so I have a dumb question:
How can I use this modal box as a really Popup? I mean, how can I make this appear every time a user enter at my site? Preferentially showing it one time per session…
Thanks in adavance!
@Mauricio Wolff yea I mentioned the fancybox on the post, but sometimes there are conflicts when working with forms and such. I had this experience with working with some .net developers.
@Iulian @Bruno
You can wrap all of the logic in the
$('a.poplight[href^=#]').click(function() { //All code that was here - move into a custom function return false; });and create a custom function like
$.fn.popOpen = function(){ //All logic goes in here };then to trigger the function you can call it out like saying
$('element').popOpen();so for example to open the link with a width of 350px:$('a.poplight[href=#?w=350]').popOpen(); //Run popOpen function once on loadTo only load once for the visitor, you need to use a cookie plugin. http://plugins.jquery.com/project/cookie
Here is a working demo w/out the cookie.
http://www.sohtanaka.com/web-design/examples/modal-window/index2.htm
@bruno, please take the time to learn the basics of jQuery before diving in in the future ;-)
How well does this work across all of the major browsers? Does this work with IE6?
@Russel This works on all modern browsers.
It also definitely works on IE6, but there are some things like the fade that do not stretch down all the way. You can fix this by getting the
$(window).height();and having a fixed height on the#fade. You will also run into png transparency issues with IE6. So yes, its functional but may not be the prettiest.Hello Soh. First of all I love your blog. Second, I love your tutorials and third I’ve got a problem with this plug. In this line:
$(‘#’ + popID).fadeIn().css({ ‘width’: Number( popWidth ) }).prepend(‘‘);
I’ve replaced “close_pop.png” with the name of an icon I have and when I pop it up it shows like this:
http://i42.tinypic.com/9jgh2r.png
See the blue border ? Why does it show like that ? Thanks and sorry for my bad english.
Greetz from Spain :)
@ViruS Thanks! and for your issue: when ever you see a blue border like that on an image, it means you have not specified a image border style. By default all images will have a border which will show once you wrap a link around it. http://www.w3schools.com/css/pr_border-style.asp
You can correct it like this
img {border: none;}I would encourage you to also check out the basic CSS tutorials as well.
Thanks a lot Soh! It worked fine :)
I like jQuery, but I suck at it … can you tell me how can I add another close button , not like the one from the corner, but a link . Something simple like Close and the additional JQuery code …
Btw, your comment form works strange, if I write a comment and click another tab, after I return to the comment tab, my comment text is reseted ( I use Google Chrome ) .
Do you know how I can pass variables from my main window to the popup window, like PHP-variables?
Nicely explained for beginners :)
It’s cool. The effect is still beautiful, and the code seems to be smaller. I love this. Thanks for sharing.
nice tutorial will try it for sure
great web & gived a backlinks…
thanks ;)
I like JQuery but sometimes in top sliding news it does not work in WP themes, and i have checked in many case………..Your code is smaller as compared to others and still good nice effects
Hey Soh, great tutorial! One question, can you do on-the-fly validation with this? Example: Once the pop up has loaded the content inside (in this case a send to a friend form) once you click the submit button is it possible for the box to NOT fade out? Every modal box plugin I have tried always disappears after I click submit. I would like to avoid iframes as much as possible so any ideas would be greatly appreciated! Thanks!
Great tutorial!… I will be very useful! Thanks =)
Any particular reason this doesn’t work with any newer jQuery library releases? I you are using 1.4.1, but the “close/fadeOut” doesn’t work with newer libraries. I am currently using 1.2.6. I tried to trouble shoot this myself, but it has be quite perplexed that EVERYTHING else works, except the close/exit feature.
)c:
@Thomas the
.live()event was added on jQuery version 1.3. So if you are using any versions older than that, it will not work.This looks great but I have a problem. And I’m new to jQuery, so be gentle. The first click changes the current page’s address but nothing else. (I know the current page address is not supposed to change.) I can use firebug and see the hidden div is there and correct. Every subsequent click opens the pop-up. Any ideas anyone?
BTW, I added an ajax call to populate the pop-up window – don’t know if that’s related but I don’t think so. I’d post the link but I put it in a debug area to test.
OK, solved my own problem. It seems I was calling a function before it was defined in the ajax called page – which is why it worked the 2nd time.
Works great! Thanks for offering this.
How can we make this non modal and something which can be moved on the web page and does remain on the top?
woohoo! i’m using this on my new site (http://mikemai.net) now. thanks so much!!!!!!
Great tutorial. I was actually looking for something like this myself so it’s much appreciated.
very kewl! will be using this in the near future.
This is awesome, at a first glance I thought it was a lightbox but its simple DIV, I think this might not work in lower browsers versions like IE 6.0, behind this awesome snippet you have shared, thanks for your valuable efforts on developing this.
Cheers!
Vivek [Founder of DeveloperSnippets – http://www.developersnippets.com
This has been real usefull mon. I’ll post a link to what I’ve been working on once I’m done , managed to mix and match some of yer fine tutorials (like the one I used to make the “mac icons” and the popup I made from this one has been great help) Working now on a “Bulletin Board” side to the page I’ll post the spaghetti code i make later mon Thanx for all this great stuff.
Hi Soh…..
This effect is just throgh css..
i was finding this one to use in website…
this is just awesome..!!
Thanks for sharing wid us….
Hi,
Brilliant tutorial, I followed it ok, but when i click the link to bring up the Popup, it works fine but when i press the close button and then click the link again, it adds another close button and everything gets pushed left, this keeps happening everytime i close and re-click the link.
Any ideas how to fix this??
EDIT: Wow now i feels stupid, i added padding to the close button, so everytime it opened the popup it padded everything 20px!
Whoops!
All sorted now…
Excellent work Soh! – As a new comer to stumble into your site, I’ve already been hooked as a fan of your work and tutorials.
There’s one recommendation I’d make… And forgive me, if this has been covered already during the comments.
The below will make the removal of the #fade div occur after the completion of the fadeOut() rather than what I noticed was occuring which was that the #fade began to fadeOut() but was cut short during animation.
I’m rather new to jQuery myself so if any masters of it have better suggestions… Please feel free to replace this with suggestions.
$(‘a.close, #fade’).live(‘click’, function() { //When clicking on the close or fade layer…
$(‘#fade , .popup_block’).fadeOut(function() { //fade them both out
$(‘#fade’).remove();
});
return false;
});
Hi Soh,
First, love your work, new site and your blogs, very cool and very helpful.
But… I was playing around and changing things with this one and noticed the popup was getting stuck half way off the screen at the top in Opera ( mine is 10.6.3 ). Its centering the top of the popup off the top of the page instead of the middle. It seems to do that for this live demo too. Is it just me or does this happen with anyone else on Opera. And WHY??? Not sure about IE but the other 7 browsers I tried they worked fine, just not in Opera! Im a bit sleepy so hope this all makes sense. The problem is real though… i think :)
Hope your going well.
I simply LOVE this!
But I have the same problem as Ben has, and I cant get it fixed.
I removed both padding and margin, but it still shows an extra close button everytime I re-open the popup.
Hy all , i love this tutorial , helped me a lot. I just want to ask you something, i am kinda at the beginning with jquery , i want to use this pop-up in a form , how can i make the pop-up to open when i click the submit button from the form ? and in the pop-up to appear the submited things from the form , something like $_POST['x'] . Thanks a lot ! and awaiting for more tutorials
I have the multiple close button issue as 2 others did above. I have not modified the code in any way (yet). Any suggestions?
In regards to my previous post (above):
Ok, I have made a discovery. The code ALWAYS prints additional close buttons. Even with Soh’s original code. You don’t notice it because it’s always floated right and it just goes right over top of the other. Look carefully and you’ll see the shadow (botton left on the circle button) get darker and darker the more you click it.
Once I fixed my overriding CSS to make it float right, it appeared to fix the duplicating issue but it doesn’t. It just hides it. I’m OK with it but it’s a little annoying.
That said… THANK YOU SO MUCH FOR THIS CODE. Very helpful and a stupid simple tutorial.
Sorry for the blog spam, but I have just resolved this issue.
Change the 4th from last line to “$(‘a.close, #fade’).remove();” — just add a.close to the list of items to remove.
Ah shoot sorry for the late reply guys, thanks for catching that Ryan and also Lecronox for your suggestion! I’ll update now. Thanks :-)
I’m also having the same effect as ISAAC but in IE7. Also, the Close button is being covered partially by the outline of the popup. You can view the work in progress here:
http://www.thegatsbysocialclub.com/index_test.php.
Anyone have any ideas?
Try adding the z-index to the img css but make it a higher value than the popup block:
img.btn_close {
float: right;
margin: -55px -55px 0 0;
z-index: 999999; // add this
}
Has anyone given this a try on an iPhone or iPad? I just checked on my iPhone. If the link is low on the page, you do not see the popup. It shows at the top of the page no matter where you’ve scrolled. Also, the #fade does not cover the entire length of the page on the iPhone. This isn’t a deal breaker, but it’s worth noting.
Also, I am using this for “premium” links on a site I’m building. A user clicks “read more” and they are presented with this popup with their purchase options. Each “read more” link takes them to a different div that is generating using some heavy lifting php code. Initially I was having a hard time with this because it was always showing the first div because of the way the js code is setup.
Here’s all you have to do to fix that:
$(this).next(‘#’ + popID).fadeIn()
I love jQuery.
I also quickly changed the popup centering, works for what i wanted to do. Might help you Craig.
css:
popup_block{
top: 0; left: 0; // changed these values from 50% to 0. Places popup top left.
}
jquery i added/changed some things, heres the full code:
//When you click on a link with class of poplight and the href starts with a #
$(‘a.poplight[href^=#]‘).click(function() {
var popID = $(this).attr(‘rel’); //Get Popup Name
var popURL = $(this).attr(‘href’); //Get Popup href to define size
//Pull Query & Variables from href URL
var query= popURL.split(‘?’);
var dim= query[1].split(‘&’);
var popWidth = dim[0].split(‘=’)[1]; //Gets the first query string value
//Fade in the Popup and add close button
$(‘#’ + popID).fadeIn().css({ ‘width’: Number( popWidth ) }).prepend(‘‘);
// ** ADDED THIS: ** //
// Get margins by subtracting the popup width/height from the window width/height, and divide by two
popMargTop=($(window).outerHeight()-$(‘#’+popID).outerHeight())/2; // outerHeight/Width includes paddings/borders
popMargLeft=($(window).outerWidth()-$(‘#’+popID).outerWidth())/2; // tip: outerHeight(true) includes margins aswell.
// ** ADDED THIS: ** //
// If margins are negative, I changed them to 0. ( I gave a top margin of 20px to fit the close button in ). This accounts for a smaller browser window than the popup size, guess you could add a scrollbar or do something else here instead.
if(popMargTop<20){popMargTop=20;}
if(popMargLeft<0){popMargLeft=0;}
//Apply Margin to Popup
$('#' + popID).css({
'margin-top' : popMargTop, // ** TOOK AWAY THE MINUS ON THESE ** //
'margin-left' : popMargLeft
});
//Fade in Background
$('body').append('’); //Add the fade layer to bottom of the body tag.
$(‘#fade’).css({‘filter’ : ‘alpha(opacity=80)’}).fadeIn(); //Fade in the fade layer – .css({‘filter’ : ‘alpha(opacity=80)’}) is used to fix the IE Bug on fading transparencies
return false;
});
I’m still learning all this stuff, probably other ways but it does seem to work for me for now.
Soh, keep up all this amazing work, i LOVE your blogs, they are all so good.
I’ll add my thanks for the code. Your work saved me several hours of labor, and the boss wants this project done “Yesterday”
Hi Soh
Could you please tell me how I would go about giving the window a set height so that content can scroll down from it?
if too much content is within the popup window it grows too large for the screen and cuts off?
how would I be able to add a scrollbar?
Many Thanks
Here’s what I want. Maybe one of you geniuses can help me.
Title (link to popup)
Teaser… Read more (popup)
(hidden div that is the popup)
I have this working for the Read more link, but I can’t figure out how to get it working on the Title link. Here’s my code:
$(this).next(‘#’ + popID).fadeIn().css({ ‘width’: Number( popWidth ) }).prepend(‘‘);
Title
Body
Read more
The read more link works but I don’t know how to get the jQuery to “find” that popup div from it’s position. Please note that I have about 15 of these Read more links taking the user to DIFFERENT, dynamically populated “popup” divs.
Thanks!
Wow, sorry for the spam… but I put pre tags around that HTML and it still rendered it.
@ANDREW:
// Check if window height is smaller than popup height:
if $(window).height()<$(‘#’+popID).outerHeight() {
// Work out new popup height ( screen height minus popup padding/border widths )
var popupHeight = $(window).height() – ($(‘#’+popID).outerHeight()-$(‘#’+popID).height());
// Set new popup height and add a scrollbar:
$(‘#’+popID).css('height', popupHeight).css('overflow-y','scroll');
}
If you have a content div inside the popup div then you might want to target that one instead with the height and scrollbar. Also, you may want/need to add "TRUE" to the outerHeight() function, which will include margins.
Hope that helps
Interesting dearrrr
hey soh, love your blog have been following and implementing your tutorials for a while now.
have you had a look at the header for this site – http://www.modnationracershq.com, it’s sweet!
hai this is viswam,i used this Inline Modal Window w/ CSS and jQuery,but iam facing some problems i.e,
i used this for signup page,in this i have a button ,i wrote code for this button in serverside,when iam clicking this button that modal window is closing,how can i prevent to stop if i click this button .if i click close button then it should be close
please help me its urgent
Thanks for this tutorial, I will be using it on a new project I am starting!
This is what I was looking for, thanks a lot mate !
Hi guys
can anyone PLEAAASE show me how to add this code to blogger?
i tried but it didn’t work, so i must’ve done something wrong…and i can’t figure it out,
i would really appreciate your help
thanks
i know i am a bit late, but some of guys/guls want this popup to load with on page load event, for that you have to do a little bit tricky amendments:
//When you click on a link with class of poplight and the href starts with a #
$(‘a.poplight[href^=#]‘).click(function() {
var popID = $(this).attr(‘rel’); //Get Popup Name
var popURL = $(this).attr(‘href’); //Get Popup href to define size
******replace this code with the below one.. *************
$(window).load(function (e) {
var popID = “popup3″; //Get hard coded Popup Name
var popURL = “#?w=506″; //Get hard coded Popup href to define size
I have simply changed the click event with the window.load,
And yes its working with ie8, chorome, and firefox…
thanks
Not work perfect in ie6
i am a beginner in CSS and jquery, i am trying to use it but i cant see the CLOSE BUTTON. there is no image or anything.
what should i do…….
and what i missed
Mine fading is not working in IE7 i dont know what is the problem……I have the same code and everything but IE7 (IEtester) is not showing fading effect. I have tried your demo page in it and it is working fine.
okay i forgot the link where you can see whats happening in the IE7, my page is not working at all………..only the fading effect….
http://www.gulzarchildcare.com/
Soh I have to say, your tutorials are awesome!
The quality of the article & result is always amazing.
<3
I have the same problem as above. The close image does not display in IE but it does in Firefox. Furthermore, any other images I include in the tag experience the same issue.
Any ideas or fix anyone?
Hi,
Is there a way to control the Popup so it automatically appears when a user lands on a page (rather then clicking on a clink) as i want to display some important information that all users should see..?
Thanks
Johnboy
thanks a lot soh! you’ve really helped me
i’m reworking the computer business’s website
i’ll give you the link when it’s done!
thanks again
Great work. I often use your site as a resource in my development.
My only suggestion would be to give the option to pull div content from a separate page.
The reason being is if someone is using this for a gallery, it is a lot of content on that one page.
Pulling from a separate page may help users manage the data more efficiently.
Thanks!
Hi guys, im using IE7 and have used this script the only problem is the popup block is loading under the the html fade rather than on top of it even though the z-index is higher the the fade z-index. Anyone got any idea’s what im missing.
Hi guys, Its OK problem solved the proble I had the popup block in the content area and it was causing the issue have moved it down to the boto of the page now and all works fine.
Any chance in getting a working version using the jQuery cookie plugin?
I have the modal window working on load perfectly. Now, I need to set a session cookie so the modal doesn’t open everytime someone hits the “home” link on my site. I want to set the cookie when the user clicks on the close modal window button. So this is my code:
$(function() {
var COOKIE_NAME = ‘poplight_cookie’;
var ADDITIONAL_COOKIE_NAME = ‘poplight_additional’;
// set cookie by session
$(‘a.poplight[href^=#]‘).click(function() {
$.cookie(COOKIE_NAME, ‘poplight’);
return false;
});
});
Unfortunately, it’s still not working. What am I missing?
Test link: test.etiquette.es
The modal javascript code was interfering with another .js so I had to disable the modal. Does anyone have the js cookie plugin working? Sorry, I got to make a deadline!
as usual great tutorial
you are genius man
Anyone have this working with the jQuery Cookie?
Anyone have a clue?
Thanks!
Has anyone find any way to display modal window centered in Opera 10.63?
I’m trying but it keeps finding some strange values for top, and left.
Best regards,
Caroline
SOH, great and light…, it Rocks, thanks.
Here what I have problem with: When using in ie = close or clicking outside closes the pop up but if the embeded code in or is video, then the ie doesn’t closes the video and the video keeps playing in the background or …?
You can see it at: RockNinja.com
I need to thank you in advance for what you have made available, and please tell us the fix for the above issue, I see some more websites having the same issue, those using this code.
Thanks and Keep Rocking…
HI, I have seen your beautiful work and commend you for a beautiful job.
Well my question is that when i insert tags in the div id it doesnt work.
Also i need to insert javascript validation, calender.
mean it displays on the main page itself instead of pop up.
could u please help me on that
prashanth
The close image does not display in IE but it does in Firefox. Furthermore, any other images I include in the tag experience the same issue.
Any ideas or fix anyone?
@MASOOD AKHTAR Thanks it works perfectly in Safari
i already have it pop up upon loading the website. my pop up is an image with an (X) close button, i removed the popup padding and background and just load the image in the pop up… my problem is how can i make the x button in my image work as a close button ?
Excellent chunk of code for putting up whatever you want instead of being holed into specific formats and jumping through hoops.
Everything looks great except on the iPad/iPhone. The window and fade pop up at the top of the page and not where your scroll position is.
I found another jquery project called prettyphoto that does pop up at current scroll position on the ipad and will re-render the popup where your scroll stops. Its a bit overkill though and was wondering if someone could gleen some tricks there to get it working here. Thanks for all the expertise and insight everyone
Very nice, but HTML Code is not w3c validate. That’s bad
How can add a scroll bar, i have text which i need to add to that pop up and that text is more then a page long.
When i use the above code, it doesn’t scroll.
Rishi
how to i get data from a popup window back to the parent window from which the pop up window was opened.
jquery beginner. please help. i have a form, i want a user to fill a text field that accepts a student’s id by clicking on a button beside the textbox that opens a pop up window from which the student can be searched by name and the studentid returned from the popup window to fill or populate the textbox
Hi SOH, Thanks for this great tutorial,
One tiny little bug though in internet explorer 6/7 is that the popup window opens below underneath the fade layer.
Any idea how I could fix this?
Regards
Alex
This blew my mind. Having set up other modals in the past, this one actually worked on the first try. Wow. Love it, thank you, bookmarked!
Hi,
I like the tutorial very much.
I am a newbie to CSS and I would welcome even a more step by step tutorials.
I am at the moment writing a Wicket framework web application and would very much like to incorporate things I see here with it.
Would be wonderful to see how to tie all these things together.
At the moment I am trying to get the inline modal window to work with my web app. and it is causing more headaches than joy.
Any pointers are welcome.
Oh, and in case I did not say so, awesome site and tutorials.
Much appreciated.
Nino
Thanks for this article, it helped me a lot. I modified your code a bit, though. I needed to show the popup window once a page loads without having a link on the page. So I did this:
$(document).ready(function() {
function popOpen(popID, popWidth) {
if ($('#' popID).length > 0) {
//Fade in the Popup and add close button
$('#' popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="/images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');
//Define margin for center alignment (vertical horizontal) – we add 80px to the height/width to accomodate for the padding and border width defined in the css
var popMargTop = ($('#' popID).height() 80) / 2;
var popMargLeft = ($('#' popID).width() 80) / 2;
//Apply Margin to Popup
$('#' popID).css({
'margin-top' : -popMargTop,
'margin-left' : -popMargLeft
});
//Fade in Background
$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer – .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies
}
};
popOpen('popup_name', '500');
//Close Popups and Fade Layer
$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer…
$('#fade , .popup_block').fadeOut(function() {
$('#fade, a.close').remove(); //fade them both out
});
return false;
});
});
And I just create the #popup_name div with PHP if I want to show a popup window with some message. I use it to show form submission messages (errors, warnings etc).
Richard Knop
hi, did anyone solved IE7 issue
Why does the x close button get cut off in the upper right corner in IE8? Does anyone have a solution for this? I’m assuming it would be CSS adjustment but of what?
@RICHARD KNOP
You’re code is a great solution, one minor piece of code… you forgot to concatenate the ‘#’ popID with a ” /// + /// symbol.
CHANGE:
if ($(‘#’ popID).length > 0) {
TO:
if ($(‘#’ + popID).length > 0) {
CHANGE:
$(‘#’ popID).fadeIn().css({ ‘width’: ….
TO:
$(‘#’ + popID).fadeIn().css({ ‘width’: ….
CHANGE:
var popMargTop = ($(‘#’ popID).height() 80) / 2;
var popMargLeft = ($(‘#’ popID).width() 80) / 2;
TO:
var popMargTop = ($(‘#’ + popID).height() 80) / 2;
var popMargLeft = ($(‘#’ + popID).width() 80) / 2;
FINALLY CHANGE:
//Apply Margin to Popup
$(‘#’ popID).css({
TO:
//Apply Margin to Popup
$(‘#’ + popID).css({
give that a shot – should work plain and spiffy.
sorry about that one more
var popMargTop = ($(‘#’ popID).height() 80) / 2;
var popMargLeft = ($(‘#’ popID).width() 80) / 2;
TO:
var popMargTop = ($(‘#’ + popID).height() + 80) / 2;
var popMargLeft = ($(‘#’ + popID).width() + 80) / 2;
LOVE everything about this site, the design, the content and the tutorials are fantastic. Bookmarked, and will be checking this site regularly, awesome work Soh, thank you and keep it up!
Can anyone fix the Internet Explorer 6/7 bug?
Hi Soh,
great tut, like always. I’m working on a project for mobile safari. Unfortunately the mobile safari browser is the worst ever (even worse than IE6, that means a lot!)
So why isn’t it possible to place the modal window in the middle of the viewport? Has anyone found a workaround to get this to work?
It would be very interesting for me and a few more, I guess…
Simply lovely! I have a problem however and I appreciate any help fixing it.
The ajax is in a file, say index.php. This can have posts and the url can become:
http://mysite.com/index.php?error=blah blah
In this case, the close button fades the countour but does not close the window. Any hints?
Anyone have problems with the popup being faded when using other styles on the page.
This is great, thanks!
I’d like to be able to put a simple xml flash slideshow in the window. Is there a way to do this? Currently the div that contains the slideshow is just invisible when I activate the popup. Or would I need to use fancybox?
DENNIS – you may need top use
!important;
on your css
WORKING COOKIE:
http://www.sohtanaka.com/web-design/inline-modal-window-w-css-and-jquery/#comment-8729
Refer back to SOH’s workaround to get a popup to load with the page.
Of course you would want to prevent this from loading everytime so:
Grab the cookie-plugin from here as SOH suggested:
http://plugins.jquery.com/project/cookie
Then in your document –
Load the cookie plugin .js BEFORE the modalwindow.js
Here’s the good stuff: open the modalwindow.js (SOH’s file – whatever you named it).
In the close function add this line:
$(‘a.close, #fade’).live(‘click’, function()
{ //When clicking on the close or fade layer…
// —— THIS IS THE LINE TO ADD
$.cookie(‘seeMeOnce’, ‘viewed’);
…..
Then wrap the popup in an if statement(again see SOH’s comment for setting this up)
// —— THIS IS THE LINE TO ADD
var onealert = $.cookie(‘seeMeOnce’);
// —— THIS IS THE LINE TO ADD
if (onealert != ‘viewed’) {
$(‘a.poplight[href=#?w=500]‘).popOpen(); //Run popOpen function once on load
// —— THIS IS THE LINE TO ADD
};
thats it- you’re all done!!!!!
Very nice!
What Browser are Supported?
really enjoyed this tutorial and it has helped alot. Trying to see if there is a way to refresh the parent content in a modal window if you happen to pull in nested links and then on reopening the original load appears. This thread: http://www.pixeline.be/blog/2008/javascript-loading-external-urls-in-jqmodal-jquery-plugin/ – has some good stuff but I don’t want to dump the nice jquery used in this modal setup. Anybody out there fooling with the same issue would love to see other solutions on this….
Mark
Very good effect
Very simple tutorial that works. Thanks for all your time and effort for putting this together and making it public.
Really great
Wonderful plug-ins, extremely useful tools.. I was wondering though if you have ever encountered the problem where you may be using 2 different versions of the JQuery.. For example I like using a very easy to understand EZ Slider plug in for the carousels but for other things.. (let’s say we like to also include a POP UP like the one you described here) well they both are using two different versions of the JQuery.. believe me this happened to me a lot and unfortunately i was never able to figure out using NOCONFLICT.. so I am stuck.. What would be your recommendations if we are in a way putting together a site almost like placing the pieces of a puzzle… wheere we have to use different plug ins and different versions of JQ… How should we proceed? Any guidance will be so appreciated..
Hi I would like to execute a custom javascript before fadeout and close popup.
Hey Soh, nice coding over there. I’ve got a question though. What would be needed to add if say, you like a div to be emptied of its DOM elements once pop up closes so that the next time, it would need to reload it instead of re-using the initial element ?
Hey there Mr. Tanaka-san.
I am trying to use FB utility with pagination.js.
html pages with tables and links in these tables are calling to the host page and when user clicks any linked word within these tables the Fb pop nice and sharp along with images and all fine, styled and looks great.
But!
When I go over to next page and trying then get back to page where I started (page 1) initially these links are not working any longer. And in address it shows this “#?w=700″. Now, unless I refresh the page it does not going off.
I am changing ID’s in order to call different content.
I’d appreciate if anyone will share some ideas
Thank you all
Hey,
The example was good enough, but did you check it with IE6, just check it out. IE6 not working for this example.
OMG! Thank you very very much!!! You saved my life with this tutorial!!
Great tut. . Exactly what I was looking for. Peace
Thanks for the tut, been looking for something like this.
Just wonder about the display:none in your css, Google may look on this as hidden text and penalise the page / site… just a thought.
I was using this attribute on a site to hide a pop up div and couldnt figure why the site was doing so badly on the searches for a niche term, looked through the code, removed the display:none from the css and within a few days was right up there, it’s widely known Google will penalise for hidden text espicially if it is keyword rich.
Great tutorial thanks!
Is there a way to force it to use a scroll bar if there is too much text to fit in the window? Currently it just fills the page and wont close!
Thanks for this article, it helped me a lot. I modified your code a bit, though. I needed to show the popup window once a page loads without having a link on the page. So I did this:
$(document).ready(function() {
function popOpen(popID, popWidth) {
if ($(‘#’ popID).length > 0) {
//Fade in the Popup and add close button
$(‘#’ popID).fadeIn().css({ ‘width’: Number( popWidth ) }).prepend(‘‘);
//Define margin for center alignment (vertical horizontal) – we add 80px to the height/width to accomodate for the padding and border width defined in the css
var popMargTop = ($(‘#’ popID).height() 80) / 2;
var popMargLeft = ($(‘#’ popID).width() 80) / 2;
//Apply Margin to Popup
$(‘#’ popID).css({
‘margin-top’ : -popMargTop,
‘margin-left’ : -popMargLeft
});
//Fade in Background
$(‘body’).append(”); //Add the fade layer to bottom of the body tag.
$(‘#fade’).css({‘filter’ : ‘alpha(opacity=80)’}).fadeIn(); //Fade in the fade layer – .css({‘filter’ : ‘alpha(opacity=80)’}) is used to fix the IE Bug on fading transparencies
}
};
popOpen(‘popup_name’, ’500′);
//Close Popups and Fade Layer
$(‘a.close, #fade’).live(‘click’, function() { //When clicking on the close or fade layer…
$(‘#fade , .popup_block’).fadeOut(function() {
$(‘#fade, a.close’).remove(); //fade them both out
});
return false;
});
});
And I just create the #popup_name div with PHP if I want to show a popup window with some message. I use it to show form submission messages (errors, warnings etc).
@ACTS7
YOU ARE THE BEST!!!! YOU FIXED PERFECTLY @RICHARD KNOP ‘S CODE!!!… IM USING IT AND IT WORKS PERFECT!!!
THANKS YOU
Hello.
Good work.
I need to display the popup under the link.
In what do i change this code :
var popMargTop = ($(‘#’ + popID).height() ) / 2;
var popMargLeft = ($(‘#’ + popID).width() ) / 2;
Thanks
Hi!!! You have an error in the demo code visible!!!
Hello,
I’m trying to have 3 pop ups, but them buttons open only the first pop div…
Is it possible to be abble to happend?
tancue
Soh – great tutorial. A problem with this though…….
If you have a pop up greater the viewport (i.e. Laptops and Netbooks), the pop up stays in a fixed position and gets cut off.
Isaac’s solution doesn’t scroll the “window”, just the content within the pop up itself (includes the close button – weird).
Has anyone else have this problem and figure out a solution to this?
So, I have made a couple changes to the code for a temporary fix. It seems that there is a need for a better solution for centering the popID. For now, this works for me:
//change this
var popMargTop = ($(‘#’ + popID).height() + 80) / 2;
//to this although this is not the correct way but works
var popMargTop = $(‘#’ + popID).height() / 2;
//then add the position – absolute to this
$(‘#’ + popID).css({
‘margin-top’ : -popMargTop,
‘margin-left’ : -popMargLeft,
‘position’ : ‘absolute’
});
This was just a quick fix to the problem of this being viewed on smaller monitors and the content getting cut off if it was larger than the users viewport. I am working on a better solution to this as there needs to be some sort of detection of the size of the users viewport and adjust accordingly.
Some one mentioned this earlier but I can’t find it now, but to fix issues in Internet Explorer (at least 7 & 8), simply put your modal DIV outside the rest of your content. If, like me, you can’t do that in your HTML (in my case because I was using a templating system and only needed this modal window on one page) then you can inert it into the DOM using jQuery like so:
$(‘body’).append(‘{your content here}’);
Do that as the first thing inside your $(document).ready() call.
Also, in my case I needed only a single modal window triggered by a single link, and I needed it to pull in the content of the linked page. This was pretty easy using an Ajax call. Here is my script in case anyone else needs to do something similar. YMMV. Note that in my case the “/nohead” that is added to the Ajax URL tells my templating system to send only the raw content of the page and none of the HTML template. You will need to fashion something similar on your own for this to work.
#register_modal_fade { /*–Transparent background layer–*/
display: none; /*–hidden by default–*/
background: #000;
position: fixed; left: 0; top: 0;
width: 100%; height: 100%;
opacity: .80;
z-index: 9999;
}
#register_modal {
display: none; /*–hidden by default–*/
padding: 0;
border: 20px solid #ddd;
float: left;
position: fixed;
top: 0; left: 50%;
z-index: 99999;
/*–CSS3 Box Shadows–*/
-webkit-box-shadow: 0px 0px 20px #000;
-moz-box-shadow: 0px 0px 20px #000;
box-shadow: 0px 0px 20px #000;
/*–CSS3 Rounded Corners–*/
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}
#register_modal img.btn_close {
float: right;
margin: -45px -50px 0 0;
}
/*–Making IE6 Understand Fixed Positioning–*/
*html #register_modal_fade {
position: absolute;
}
*html #register_modal {
position: absolute;
}
$(document).ready(function() {
$(‘body’).append(”);
$(‘#register_link’).click(function() {
var popURL = $(this).attr(‘href’); //Get Popup href to define size
var popWidth = 700; // Fixed width of 700px;
//Fade in the Popup and add close button
$(‘#register_modal’)
.fadeIn()
.css({ ‘width’: Number( popWidth ) })
.prepend(‘‘);
$(‘#register_content’).load(popURL+’/nohead’, hijackRegForm);
//Define margin for center alignment (vertical horizontal) – we add 60px to the height/width to accomodate for the padding and border width defined in the css
var popMargLeft = ($(‘#register_modal’).width() + 60) / 2;
var popMaxHeight = $(window).height() – 100;
//Apply Margin to Popup
$(‘#register_modal’).css({
‘margin-top’ : 30,
‘margin-bottom’ : 30,
‘margin-left’ : -popMargLeft,
‘height’ : popMaxHeight
});
$(‘#register_content’).css({
‘height’ : popMaxHeight,
‘overflow’ : ‘auto’
});
//Fade in Background
$(‘body’).append(”); //Add the fade layer to bottom of the body tag.
$(‘#register_modal_fade’).css({‘filter’ : ‘alpha(opacity=80)’}).fadeIn(); //Fade in the fade layer – .css({‘filter’ : ‘alpha(opacity=80)’}) is used to fix the IE Bug on fading transparencies
return false;
});
//Close Popups and Fade Layer
$(‘a.close’).live(‘click’, function() { //When clicking on the close or fade layer…
$(‘#register_modal_fade , #register_modal’).fadeOut(function() {
$(‘#register_modal_fade, a.close’).remove(); //fade them both out
});
$(‘#register_content’).html(”);
return false;
});
});
function hijackRegForm() {
$(‘#register_registration_form’).submit(function(event){
event.preventDefault();
$.post($(this).attr(‘action’)+’/nohead’, $(this).serialize(), function(html){
$(‘#register_content’).html(html).scrollTop(0);
hijackRegForm();
});
});
}
Register
Let me try to paste that code again:
<style type="text/css">
#register_modal_fade { /*–Transparent background layer–*/
display: none; /*–hidden by default–*/
background: #000;
position: fixed; left: 0; top: 0;
width: 100%; height: 100%;
opacity: .80;
z-index: 9999;
}
#register_modal {
display: none; /*–hidden by default–*/
padding: 0;
border: 20px solid #ddd;
float: left;
position: fixed;
top: 0; left: 50%;
z-index: 99999;
/*–CSS3 Box Shadows–*/
-webkit-box-shadow: 0px 0px 20px #000;
-moz-box-shadow: 0px 0px 20px #000;
box-shadow: 0px 0px 20px #000;
/*–CSS3 Rounded Corners–*/
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}
#register_modal img.btn_close {
float: right;
margin: -45px -50px 0 0;
}
/*–Making IE6 Understand Fixed Positioning–*/
*html #register_modal_fade {
position: absolute;
}
*html #register_modal {
position: absolute;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
$('body').append('<div id="register_modal"><div id="register_content"></div></div>');
$('#register_link').click(function() {
var popURL = $(this).attr('href'); //Get Popup href to define size
var popWidth = 700; // Fixed width of 700px;
//Fade in the Popup and add close button
$('#register_modal')
.fadeIn()
.css({ 'width': Number( popWidth ) })
.prepend('<a href="#" class="close"><img src="/images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');
$('#register_content').load(popURL '/nohead', hijackRegForm);
//Define margin for center alignment (vertical horizontal) – we add 60px to the height/width to accomodate for the padding and border width defined in the css
var popMargLeft = ($('#register_modal').width() 60) / 2;
var popMaxHeight = $(window).height() – 100;
//Apply Margin to Popup
$('#register_modal').css({
'margin-top' : 30,
'margin-bottom' : 30,
'margin-left' : -popMargLeft,
'height' : popMaxHeight
});
$('#register_content').css({
'height' : popMaxHeight,
'overflow' : 'auto'
});
//Fade in Background
$('body').append('<div id="register_modal_fade"></div>'); //Add the fade layer to bottom of the body tag.
$('#register_modal_fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer – .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies
return false;
});
//Close Popups and Fade Layer
$('a.close').live('click', function() { //When clicking on the close or fade layer…
$('#register_modal_fade , #register_modal').fadeOut(function() {
$('#register_modal_fade, a.close').remove(); //fade them both out
});
$('#register_content').html('');
return false;
});
});
function hijackRegForm() {
$('#register_registration_form').submit(function(event){
event.preventDefault();
$.post($(this).attr('action') '/nohead', $(this).serialize(), function(html){
$('#register_content').html(html).scrollTop(0);
hijackRegForm();
});
});
}
</script>
<a href="/register" id="register_link">Register</a><br/>
This tutorial is excellent but i have been ramming my head against the wall for 2 days now trying to customise it a bit.
I can code html and css but when it comes to jquery i have no idea :(
All i want to do is have the modal box load automatically after say 1 minute.
Is this possible and does anyone know how to do it pleeeeease?
Hey, I’m using this on my page – I have two buttons on the side of my site that open a pop up. When I open my button facebook, the ‘x’ in the right corner comes up fine but when I exit and then open the button twitter, the ‘x’ isn’t there.
My site is http://blog.zahrinaphotography.com/
Any ideas where I’m going wrong?
Cheers
Hi,
Im no expert so if this doesnt work then sorry but if you add this…
$(‘a.close’).fadeIn();
after this…
//Fade in Background
$(‘body’).append(”); //Add the fade layer to bottom of the body tag.
$(‘#fade’).css({‘filter’ : ‘alpha(opacity=80)’}).fadeIn(); //Fade in the fade layer
This tutorial has been soooo helpful – especially since I’m such a noob. Through trial and error I have managed to put together a beautiful screen that opens on load, has the scroll for the large doc it contains and everything is working great – except one thing.
I have placed 2 push buttons (accept/decline) at the end of the doc (replacing the close button/image). I would like the “accept” button to close the modal screen and remain on the current page & “decline” button to redirect to another page but I can’t figure out how to define them to get them to work with the screen. Any help would be greatly appreciated………………..
Hi Ruth,
If you post the code on here i will update it for you so that the buttons will do this.
Also i need the code to load the modal on page load please??
Hi Craig
Thanks so much for your response but amazingly I figured it out. Here’s how:
I defined my buttons like this:
and then I defined the “accept” button within the modal.js file. I will send the entire code so you can see how I defined the window open and the button also. You will notice that I used Masood Akhtar advice (for the auto open) from his post (06-02-10) and it turned out great. Hope this helps…………
$(document).ready(function() {
$(window).load(function (e) {
var popID = ‘popup3′; //Get Popup Name
var popURL = ‘#?w=600′; //Get hard coded Popup href to define size
//Pull Query & Variables from href URL
var query= popURL.split(‘?’);
var dim= query[1].split(‘&’);
var popWidth = dim[0].split(‘=’)[1]; //Gets the first query string value
//Fade in the Popup
$(‘#’ + popID).fadeIn().css({ ‘width’: Number( popWidth ) }).prepend();
//Define margin for center alignment (vertical horizontal) – we add 80px to the height/width to accomodate for the padding and border width defined in the css
var popMargTop = ($(‘#’ + popID).height() + 80) / 2;
var popMargLeft = ($(‘#’ + popID).width() + 80) / 2;
//Apply Margin to Popup
$(‘#’ + popID).css({
‘margin-top’ : -popMargTop,
‘margin-left’ : -popMargLeft
});
//Fade in Background
$(‘body’).append(”); //Add the fade layer to bottom of the body tag.
$(‘#fade’).css({‘filter’ : ‘alpha(opacity=30)’}).fadeIn(); //Fade in the fade layer – .css({‘filter’ : ‘alpha(opacity=30)’}) is used to fix the IE Bug on fading transparencies
return false;
});
//Close Popups and Fade Layer
$(‘#accept, #fade’).live(‘click’, function() { //When clicking on the close or fade layer…
$(‘#fade , .popup_block’).fadeOut(function() {
$(‘#fade, a.close’).remove(); //fade them both out
});
return false;
});
});
Ooopps! I don’t seem to be able to follow instructions either! :)
The following is how I defined the buttons………….
Thanks, this was helpful :o)
your the bombs Soh! Thanks for the handy code! :)
LOVE everything about this site, the design, the content and the tutorials are fantastic. Bookmarked, and will be checking this site regularly, awesome work So, thank you and keep it up.. thanks for sharing..
@Richard Knop! What do you mean with #popup_name div? I need to show a confirmation message after form submission.
i did like that, but its not working:
div id=”popup_name” class=”popup_block”>
Popup #1
can anybody explain how can I show this modal window after a form submission?
I need the modal window only after a correct form submission, I tried the following code, but it shows the modal window also on a first tim page load, how can i avoid this?
$(document).ready(function() {
$(‘:submit’).click(popOpen(‘popup_name’, ’500′));
function popOpen(popID, popWidth) {
//Fade in the Popup and add close button
$(‘#’ + popID).fadeIn().css({ ‘width’: Number( popWidth ) }).prepend(‘‘);
//Define margin for center alignment (vertical horizontal) – we add 80px to the height/width to accomodate for the padding and border width defined in the css
var popMargTop = ($(‘#’ + popID).height() + 80) / 2;
var popMargLeft = ($(‘#’ + popID).width() + 80) / 2;
//Apply Margin to Popup
$(‘#’ + popID).css({
‘margin-top’ : -popMargTop,
‘margin-left’ : -popMargLeft
});
//Fade in Background
$(‘body’).append(”); //Add the fade layer to bottom of the body tag.
$(‘#fade’).css({‘filter’ : ‘alpha(opacity=80)’}).fadeIn(); //Fade in the fade layer – .css({‘filter’ : ‘alpha(opacity=80)’}) is used to fix the IE Bug on fading transparencies
};
//Close Popups and Fade Layer
$(‘a.close, #fade’).live(‘click’, function() { //When clicking on the close or fade layer
$(‘#fade , .popup_block’).fadeOut(function() {
$(‘#fade, a.close’).remove(); //fade them both out
});
return false;
});
});
Thank you for the code. It works great.
Is there a way to modify it so that instead of clicking on a link, the modal can be displayed on pageload?
Next, i’d like to take it a step further and have it check for a cookie to see if the user has already seen the modal. If it has, it skips the modal. If it hasn’t, the modal is loaded. I found some code for the cookie, but I can’t get it to work 100% in my sandbox.
Speak Your Mind...