»
EnglishFrenchVietnamese

Print - Opening New Windows - JavaScriptBank.com

Full version: jsB@nk » Snippet » Opening New Windows
URL: https://www.javascriptbank.com/opening-new-windows.html

Opening New Windows © JavaScriptBank.comWhen using strict doctypes the target attribute is not allowed for opening new windows. This JavaScript will accomplish that task easily and unobtrusively. The script uses the class name "non-html" so you will need to add that to the links you want to open in a new window, e.g., <a href="yourDocument.html" class="non-html">A sample file</a>This JavaScript will not open a new window if a modifier key (ctrl/shift/etc.) is pressed while the JavaScript link is clicked. It also checks if the new window was successfully opened. If it was not, it returns true to allow the browser to open the JavaScript link in the original window.

Full version: jsB@nk » Snippet » Opening New Windows
URL: https://www.javascriptbank.com/opening-new-windows.html



JavaScript
<script language="javascript">/* addEvent function fromhttp://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html */function addEvent( obj, type, fn ) { if (obj.addEventListener)  obj.addEventListener( type, fn, false ); else if (obj.attachEvent) {  obj["e"+type+fn] = fn;  obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }  obj.attachEvent( "on"+type, obj[type+fn] ); }}function removeEvent( obj, type, fn ) { if (obj.removeEventListener)  obj.removeEventListener( type, fn, false ); else if (obj.detachEvent) {  obj.detachEvent( "on"+type, obj[type+fn] );  obj[type+fn] = null;  obj["e"+type+fn] = null; }}/* Create the new window */function openInNewWindow(e) { var event; if (!e) event = window.event; else event = e; // Abort if a modifier key is pressed if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {  return true; } else {  // Change "_blank" to something like "newWindow" to load all links in the same new window      var newWindow = window.open(this.getAttribute('href'), '_blank');  if (newWindow) {   if (newWindow.focus) {    newWindow.focus();  }  return false;  }  return true; }}/* Add the openInNewWindow function to the onclick event   of links with a class name of "new-window" */function getNewWindowLinks() { // Check that the browser is DOM compliant if (document.getElementById && document.createElement && document.appendChild) {  // Change this to the text you want to use to alert the user that a new window will be opened  var strNewWindowAlert = " [ file opens in a new window ]";  // Find all links  var links = document.getElementsByTagName('a');  var objWarningText;  var link;  for (var i = 0; i < links.length; i++) {   link = links[i];   // Find all links with a class name of "non-html"   if (/\bnon\-html\b/.test(link.className)) {    // Create an em element containing the new window warning text and insert it after the link text    objWarningText = document.createElement("em");    objWarningText.appendChild(document.createTextNode(strNewWindowAlert));    link.appendChild(objWarningText);    link.onclick = openInNewWindow;   }  }  objWarningText = null; }}addEvent(window, 'load', getNewWindowLinks);</script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


HTML
<a href="http://javascriptbank.com/" class="non-html">Sampled by JavaScriptBank.com</a><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->