このページの目次

a 要素の target="_blank" の代わり

概要

a 要素の class 属性に外部のという意味である "external" が指定されているか、pdf, doc, xls, ppt ファイルにリンクしている場合、リンク先を別ウインドウで開くことのできる JavaScript です。(ただし、設定でクラス名、ファイルの種類は自由に変えられます。)

使い方

ダウンロードした openwin.js を XHTML ファイルの head 要素内などで読み込んでください。

<script type="text/javascript" src="js/openwin.js"></script>

openwin.jsのソース内の

conf : {
	className : "external",//リンク先を別ウインドウで開きたいa要素につけるclass名
	fileTypes : ["pdf","doc","xls","ppt"]//リンク先を別ウインドウで開きたいファイルの拡張子
},

を変更することで、設定の変更が行えます。fileTypes の個数は任意で増減自由です。

ソースコード

/*====================================================================================================
//////////////////////////////////////////////////////////////////////////////////////////////////////

	Author : http://www.yomotsu.net
	created: 2007/02/01
	update : 2007/11/15
	Licensed under the GNU Lesser General Public License version 2.1

//////////////////////////////////////////////////////////////////////////////////////////////////////
====================================================================================================*/
var yomotsuOpenWin = {
	
	conf : {
		className : "external",//リンク先を別ウインドウで開きたいa要素につけるclass名
		fileTypes : ["pdf","doc","xls","ppt"]//リンク先を別ウインドウで開きたいファイルの拡張子
	},

	main : function(){
		var fileTypesReg = "";
		for(i = 0; i <yomotsuOpenWin.conf.fileTypes.length; i++){
			fileTypesReg += "."+yomotsuOpenWin.conf.fileTypes[i]+"|"
		}
		fileTypesReg=fileTypesReg.slice(0, -1);
	
		var a = document.links;
		for (i = 0; i <a.length; i++) {
			if (new RegExp("\\b" + yomotsuOpenWin.conf.className + "\\b").exec(a[i].className)||
		    	(new RegExp(fileTypesReg).exec(a[i].getAttribute("href")))){
				a[i].onclick = yomotsuOpenWin.openWin;
				a[i].onkeypress = yomotsuOpenWin.openWin;
			}
		}
	},
		
	openWin : function(){
		var uri = this.href;
		window.open(uri, "", "toolbar=yes, location=yes, directories=yes, status=yes, menubar=yes, scrollbars=yes, resizable=yes, close=yes");
		this.href = uri;
		return false;
	},
	
	addEvent : function(){
		try {
			window.addEventListener('load', yomotsuOpenWin.main, false);
		} catch (e) {
			window.attachEvent('onload', yomotsuOpenWin.main);
		}
	}
	
}

yomotsuOpenWin.addEvent();


トラックバックURI

http://www.yomotsu.net/lab/javascripts/openwin/trackback

この記事へのコメントはまだありません

コメントフォーム

コメント

コメントでは一部の XHTML タグを使用できます。 : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

実験室に戻る

著作権情報等

Copyright © 2004-2008 yomotsu-net. Some Rights Reserved.