Skip to content

Instantly share code, notes, and snippets.

@sthembi
Created September 22, 2011 14:41
Show Gist options
  • Save sthembi/1234940 to your computer and use it in GitHub Desktop.
Save sthembi/1234940 to your computer and use it in GitHub Desktop.
Open links and Popups in a new window - with jQuery
Since "target" is not valid - use this for popups with jQuery
Using the current W3 standard for anchor tags anchor tags can be given a rel attribute that describes what relation the linked-to page has to this current page. It also provides a standard list of available relationships. Many other groups added to this list and there are many widely-used values for the rel tag, including "external".
With this method, give links to be made popups a rel attribute of "popup", and include the following file in the head of the document.
// ----------------------------------------------------------------------------
// links and popups
// ----------------------------------------------------------------------------
// open external links in a new window ("target" is not valid XHTML strict)
function externallinks() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
anchor.tabindex = i;
if (anchor.getAttribute("href")) {
if (anchor.getAttribute("rel") == "external")
anchor.target = "_blank";
else if (anchor.getAttribute("rel") == "parent")
anchor.target = "_parent";
else if (anchor.getAttribute("rel") == "top")
anchor.target = "_top";
else if (anchor.getAttribute("rel") == "popup")
$('a[rel="popup"]').click(function(){return $.dbPopWin( $(this).attr('href'));}
);
}
}
}
// dbPopWin: db Popup Windows Plugin for JQuery
jQuery.dbPopWin = function(url, options)
{options = jQuery.extend(
{
/* default options */
dbPopWinWidth: 480,
dbPopWinHeight: 300,
dbPopWinTarget: 'dbPopWin',
dbPopWinScrollbars: 'yes',
dbPopWinResizable: 'no',
dbPopWinMenuBar: 'no',
dbPopWinAddressBar: 'no'
},
options
);
/* center the window by default. */
if (!options.dbPopWinY){options.dbPopWinY = screen.height / 2 - options.dbPopWinHeight / 2;};
if (!options.dbPopWinX){options.dbPopWinX = screen.width / 2 - options.dbPopWinWidth / 2;};
open(
url,
options['dbPopWinTarget'],
'width= ' + options.dbPopWinWidth +
',height=' + options.dbPopWinHeight +
',top=' + options.dbPopWinY +
',left=' + options.dbPopWinX +
',scrollbars=' + options.dbPopWinScrollbars +
',resizable=' + options.dbPopWinResizable +
',menubar=' + options.dbPopWinMenuBar +
',location=' + options.dbPopWinAddressBar
);
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment