Click here to Skip to main content
15,892,005 members
Articles / Programming Languages / C#
Article

Starting your website in a popup window

Rate me:
Please Sign up or sign in to vote.
2.68/5 (10 votes)
2 Jul 2008CPOL1 min read 28.7K   307   22   8
You can start your website in a popup window (without having user to click even a single button)

Introduction

ASP.net has provided very rich environment for web development; and its keep on improving the development experience.

Most of the times we come across a problem to compare the look & feel of windows application with web application (because our clients ask us to do that :( ) and I am sure that it might have happened with most of us (especially developers working on re-engineering projects)

Details

We were in need to find a solution to open very first page of our site as a popup window, and the only solution I got is to take help from my girlfriend...... i.e. Javascript :)

Using the code

Using the following steps, you can easily achieve this feature.

  1. Add a default.htm page in root of your web project.
  2. Write the javascript code on top of that page (ref. attached project also).
  3. Give name & path (if required) of the page you want to open in popup (most of times its default.aspx)
  4. Make default.htm as start page for that site.

Hooray.... you have successfully configured popup functionality for you website.

<script type="text/javascript" language="'javascript'">
    window.onload = poponload; 
    function poponload()
    {   
        try
        {                                
            //Set variables, version is IE version number
            var ua = window.navigator.appVersion;
            var msie = ua.indexOf ( "MSIE " );
            var version = ua.substring(msie+5,msie+6);
            //Open new window
            var now = new Date();
            var hour        = now.getHours();
            var minute      = now.getMinutes();
            var second      = now.getSeconds();
            var monthnumber = now.getMonth();
            var monthday    = now.getDate();
            var year        = now.getYear();
            var winName='master'+year+monthnumber+hour+minute+second;/*making window name dynamic*/
            /*fo in query parameter is used to tell that this site is first opening, returnURL will be used in case of forms authentication*/
            var win = window.open ('default.aspx?fo=1', winName, "fullscreen=no,scrollbars=1,resizable=1,status=1,dependent=yes,alwaysRaised=yes");
            if(!win)
            {
                alert("Popups are blocked.\nMust allow popups to run the application");
            }
            else
            {              
                //Check version number and run correct code
                if (version >= "7")
                {
                    if (win) 
                    {
                        window.open('', '_parent','');
                        window.close();
                    }
                    
                } 
                else 
                {
                    self.opener = this;
                    setTimeout('self.close()',500);
                };
            }
        }
        catch(ex){alert(ex.message);}
    }
</script>

Checking Popups are blocked

var win = window.open ('default.aspx?fo=1', winName, "fullscreen=no,scrollbars=1,resizable=1,status=1,dependent=yes,alwaysRaised=yes");
            if(!win)
            {
                alert("Popups are blocked.\nMust allow popups to run the application");
            } 

You can also use the above code snippet in your application only to check that whether any respective popup is opening in browser or it is blocked by the browser / google toolbar.

Browsers Compatible

  • IE6 +

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Programming is my passion and C# is my friend, Javascript is my first crush..
Surprized!!!, don't be....
I love doing programming and writing efficient code, with the help of code project; I am able to share my experiences too with you all.

Thanks for visiting and voting this post.. [Smile | :) ]

Comments and Discussions

 
GeneralThis is Great! Pin
Heywood3-Jul-08 3:49
Heywood3-Jul-08 3:49 
GeneralRe: This is Great! Pin
Surjit Singh Dadhwal29-Jul-08 10:59
Surjit Singh Dadhwal29-Jul-08 10:59 
Generalgreate Pin
Abhijit Jana3-Jul-08 0:41
professionalAbhijit Jana3-Jul-08 0:41 
GeneralYour article really helped us!!! Keep it up. Pin
Vinit Kumar Singh26-May-08 23:09
Vinit Kumar Singh26-May-08 23:09 
General[Message Removed] Pin
Mojtaba Vali23-May-08 21:59
Mojtaba Vali23-May-08 21:59 
Spam message removed
GeneralRe: starting step Pin
Surjit Singh Dadhwal25-May-08 19:37
Surjit Singh Dadhwal25-May-08 19:37 
GeneralNo visible use Pin
Chris_Thomas23-May-08 13:05
Chris_Thomas23-May-08 13:05 
GeneralRe: No visible use Pin
Surjit Singh Dadhwal25-May-08 19:41
Surjit Singh Dadhwal25-May-08 19:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.