Open New Browser Window with jQuery (Custom Size)

coldfusion actionscriptI sure this is nothing special but I thought I would share. I’ve been play around with jQuery and wanted to build an easy way to open a new browser window with a certain size for multiple links. Below is what I came up with. Click on the links below to see a simple demo.

click me 1 (fixed size)
click me 2 (fixed size w/ scrollbar)

 

Update : I have added a new and improved version at the end of this article.

Everything Together

First take a look at what the whole thing and later I will break it out.

<html>
    <head>
    	<title></title>

    	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script>

        <script type="text/javascript">
            var windowSizeArray = [ "width=200,height=200",
									"width=300,height=400,scrollbars=yes" ];

            $(document).ready(function(){
                $('.newWindow').click(function (event){

                    var url = $(this).attr("href");
                    var windowName = "popUp";//$(this).attr("name");
                    var windowSize = windowSizeArray[$(this).attr("rel")];

                    window.open(url, windowName, windowSize);

                    event.preventDefault();

                });
            });
        </script>
    </head>

    <body>
        <a href="http://www.flashmn.com/" rel="0" class="newWindow" >click me</a>
        <a href="http://www.mnswf.com/" rel="1" class="newWindow" >click me</a>
    </body>
</html>

Add the HTML Links

Lets start with our links in the of our html. Notice I used the “rel” attribute to determine the array slot I would like to use. I will be using an array to hold on the link information.

<a href="http://www.flashmn.com/" rel="0" class="newWindow" >click me</a>
<a href="http://www.mnswf.com/" rel="1" class="newWindow" >click me</a>

Include the jQuery JavaScript library

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script>

Include the jQuery Script

Place this script below the include of the jQuery library. Below I explain what each line does.

<script type="text/javascript">
	var windowSizeArray = [ "width=200,height=200",
							"width=300,height=400,scrollbars=yes" ];

	$(document).ready(function(){
		$('.newWindow').click(function (event){

			var url = $(this).attr("href");
			var windowName = "popUp";//$(this).attr("name");
			var windowSize = windowSizeArray[ $(this).attr("rel") ];

			window.open(url, windowName, windowSize);

			event.preventDefault();

		});
	});
</script>

The jQuery Script Explained

<script type="text/javascript">
	//Created an array to hold onto the desired info.
	var windowSizeArray = [ "width=200,height=200",
							"width=300,height=400,scrollbars=yes" ];

	$(document).ready(function(){
		//Links that has the ".newWindow" class will call this script.
		$('.newWindow').click(function (event){

			// Gets the URL from the clicked link.
			var url = $(this).attr("href");

			/*Gets the name from the clicked link. Currently I commented out the
			  jquery script and just put "popUp" for a default name because I didn't
              include the name in the links.*/
			var windowName = "popUp";//$(this).attr("name");

			/*Places the string from the array into the windowSize variable.
			  The array slot is determined by the "rel" number on the link.*/
			var windowSize = windowSizeArray[  $(this).attr("rel")  ];

			//This method opens a new browser window.
			window.open(url, windowName, windowSize);

			/*Prevents the browser from executing the default action and
			  allows us to use the "window.open" within our script.*/
			event.preventDefault();

		});
	});
</script>

New and Improved Version

Below I use the data attributes to set the size of the popup window. This way is more dynamic because you do not need to have extra logic in the JavaScript.

<html>
<head>
    <title></title>

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.1.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function(){
            $('.js-newWindow').click(function (event) {
                event.preventDefault();

                var $this = $(this);

                var url = $this.attr("href");
                var windowName = "popUp";
                var windowSize = $this.data("popup");

                window.open(url, windowName, windowSize);
            });
        });
    </script>
</head>

<body>
    <a href="http://www.typescriptlang.org/" class="js-newWindow" data-popup="width=200,height=200">click me</a>
    <a href="http://nodejs.org/" class="js-newWindow" data-popup="width=300,height=500,scrollbars=yes">click me</a>
</body>
</html>

46 Responses to “Open New Browser Window with jQuery (Custom Size)”

  1. Your code is just what I was looking for!

    Many thanks,

    Vanessa

  2. Veronica Garcia Says:
    March 19, 2011 at 6:10 pm

    Is there a way to position the window in the center of the screen?

    • I would have to change the code a little but to get you started you could do something like this:

      var windowWidth = 200;
      var windowHeight = 200;
      var windowLeft = parseInt((screen.availWidth/2) - (windowWidth/2));
      var windowTop = parseInt((screen.availHeight/2) - (windowHeight/2));
      var windowSize = "width=" + windowWidth + ",height=" + windowHeight + "left=" + windowLeft + ",top=" + windowTop + "screenX=" + windowLeft + ",screenY=" + windowTop;

  3. Andrea Ballerino Says:
    July 10, 2011 at 3:36 am

    Very useful, thanks!

  4. It works like a charm …
    I was having trouble using javaScript window.open which was not working properly in IE (I could not use spaces in link name). Someone suggested using jQuery

  5. That is what i am looking for !

    Thank!

  6. Great script
    Is there any way I could pass values to the new window?
    eg. I’d get… var job_id = $(this).attr(“id”); ….and would like to pass the job_id value to the new window using this script.
    Many thanks

  7. Many thanks for this extremely useful code!

  8. well explained! thanks bro!

  9. thanks very much, this code is very useful, simple, and easy to understand

  10. Shyam Sundar Says:
    November 18, 2011 at 11:26 am

    Awesome thank you.

  11. It’s working. Thanks!!! I was sarching this for an hour.

  12. This is a fantastic improvement on the current JS code I am using. I will also definitely be using the centre-screen code you replied to Veronica with.

    Is there a way of removing the address bar in the new window?

    Many Thanks

  13. Ei man thanks for share! :D

  14. Thank you! Saved me time from trial and error!

  15. Thank u Man !!!!!

  16. Hi,

    Jquery has undoubtedly provided solutions where javascript complex knowledge was required, I faced a similar problem with window.open functionality, tried to place a simple onClick function on a button to open a child popup, but in some IE it worked (at least for me) and some users faced a lot of problems in the same. (crazy stuff) . Still jquery managed to rescue me out of the situations and worked even with lof of bad stuffed cookies in careless users machines.

    There is also one more behavior that I think jquery can improve is with the IE compatibility view settings, in this area IE disables the default behavior of jquery, if anybody has any idea, do lemme know…cheers…

  17. Thanks a lot bro :)

  18. thanks bro…great.

  19. Ashok Parmar Says:
    March 22, 2012 at 10:14 pm

    Thanks for share this info!!!

  20. Great! Thank Robert.

    I was trying for window center

    code:-

    $(document).ready(function(){
    $(‘.newWindow’).click(function (event){
    var windowWidth = 700;
    var windowHeight = 600;
    var windowLeft = parseInt((screen.availWidth/2) – (windowWidth/2));
    var windowTop = parseInt((screen.availHeight/2) – (windowHeight/2));
    var windowSizeArray = [ “width=700,height=600”, “width=windowWidth,height=windowHeight,scrollbars=yes,top=’ + windowTop + ‘,screenX=’ + windowLeft + ‘,screenY=’+ windowTop’ “];
    var url = $(this).attr(“href”);
    var windowName = “popUp”;//$(this).attr(“name”);
    var windowSize = windowSizeArray[$(this).attr(“rel”)];
    window.open(url, windowName, windowSize);
    event.preventDefault();

    });
    });

    What have I wroing?

  21. Hi,

    This is what i want, I need one change to hide the address bar.

    I put the following code:

    var windowSizeArray = [ “width=1000,height=700,scrollbars=yes,status=no,toolbar=no,menubar=no,fullscreen=yes,location=no”]

    Is there any parameter to hide the address bar with this script?

  22. thanks for sharing this awesome script….

  23. Many, many thanks!

  24. Thank you so much, this is exactly what I needed.

  25. Thanks!

  26. can we have some alternate jquery to open a new window without using window.open().I asking becoz wndow.open will work properly in IE but it wont work in Firefox.
    So can u share any code for that.

  27. Thank you, It is a very good tutorial :)

  28. THANK YOU!!! This helped me so much. :)

  29. hemachandran Says:
    December 7, 2012 at 4:21 am

    Thank for sharing….

  30. it is great! thank you very much and best regards.

  31. First of all thank you for making this script available! It works perfectly. Three questions…

    In an attempt to position the new window I use the syntax ‘window.moveTo(20,20);’
    and the parent window moves not the new window. How can I correct this? Also is there a way to assign a background color to the new window? Lastly is it possible to assign the class .newWindow to the div containing the links instead of each link?

    Once again, thanks for making this available!

  32. this cool, thanks

  33. What if I want to use a form button to open a new window instead of a link, I think I need to Google some more. ;-)

  34. Thank u so much! This was extremely useful… still useful in 2013!

  35. I would like this to happen without clicking any links…

    Trying to make it happen after: $(document).ready(function(){

    Can anybody help?

  36. Thank you, I’ll be using that in my next project!

  37. Thank you very much, very neat and clean code.
    No hassles, just what I was looking for.

  38. thank!

  39. is it possible..
    if we click on the links then this link will open in another browser not in same browser window ??

  40. Thank you so much! almost 5 years and its working like a charm…

  41. LOVELY! :D Works like a charm

  42. Thank you very much :)

  43. This is great. Is there a way in the newer version to have a default size but then be able to change with the data parameters?

Leave a Reply