找回密码
 注-册

QQ登录

只需一步,快速开始

查看: 1725|回复: 0

[特效] WEB标准学习:自定义打开链接的方式

[复制链接]
Leya 发表于 2008-4-21 19:25:02 | 显示全部楼层 |阅读模式
一个链接小实例,由于在XHTML1.0过渡型标准之前,target="_blank"属性一直是点击链接后弹出新窗口的方法,但是XHTML1.0严格型标准以及XHTML1.1之后就不再支持target属性,也就是说不能再用target="_blank"来弹出一个新的窗口!本实例探讨的就是在XHTML1.1标准下弹出新窗口的方法,并结合Cookies实现客户端自定义弹出新窗口的方法。
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>自定义打开链接的方式</title>
  6. <style type="text/css">
  7. <!--
  8. #main {
  9.     width: 600px;
  10.     margin-right: auto;
  11.     margin-left: auto;
  12.     border: 1px solid #006699;
  13.     height: 600px;
  14. }
  15. #title {
  16.     background-color: #006699;
  17.     padding: 10px;
  18.     width: 100%;
  19.     font-family: Verdana, Arial;
  20.     font-size: 16px;
  21.     color: #FFFFFF;
  22.     font-weight: bold;
  23. }
  24. #property {
  25.     margin: 10px;
  26.     border: 1px solid #99CC00;
  27.     height: 24px;
  28.     background-color: #F8FEE9;
  29.     font-size: 12px;
  30.     line-height: 24px;
  31. }
  32. #link {
  33.     margin: 10px;
  34.     border: 1px solid #0099CC;
  35.     background-color: #EFEEFF;
  36.     font-size: 12px;
  37.     height: 366px;
  38.     padding: 20px;
  39. }
  40. #copyright {
  41.     font-size: 12px;
  42.     padding: 20px;
  43.     color: #003333;
  44.     line-height: 16px;
  45. }
  46. img{
  47.     border-top-style: none;
  48.     border-right-style: none;
  49.     border-bottom-style: none;
  50.     border-left-style: none;
  51. }
  52. .center {
  53.     text-align: center;
  54. }
  55. -->
  56. </style>
  57. <script type="text/javascript">
  58. <!--
  59. function onlinks(){

  60. if(getCookie("links")=="_blank"){
  61. document.getElementById("c1").checked=true;
  62. document.getElementById("c2").checked=true;
  63. newlinks();//dolinks();
  64. }
  65. else{
  66. document.getElementById("c1").checked=false;
  67. document.getElementById("c2").checked=false;
  68. newlinks();//dolinks();
  69. }
  70. }

  71. function dolinks(){
  72. document.getElementById("c2").checked?setCookie('links','_blank',1):deleteCookie('links');
  73. }

  74. function newlinks() {
  75. document.getElementById("c1").checked?fc1():fc2()
  76. function fc1(){
  77. linktarget="_blank";document.getElementById("cookies_p").style.display="block"}
  78. function fc2(){
  79. linktarget="";document.getElementById("cookies_p").style.display="none"}

  80. var anchors = document.getElementsByTagName("a");
  81. for (var i=0; i<anchors.length; i++) {
  82.    var anchor = anchors[i];
  83.    if (anchor.getAttribute("href"))
  84.      anchor.target = linktarget;
  85. }
  86. }

  87. function getCookie(name) {
  88.     var start = document.cookie.indexOf( name + "=" );
  89.     var len = start + name.length + 1;
  90.     if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
  91.         return null;
  92.     }
  93.     if ( start == -1 ) return null;
  94.     var end = document.cookie.indexOf( ";", len );
  95.     if ( end == -1 ) end = document.cookie.length;
  96.     return unescape( document.cookie.substring( len, end ) );
  97. }   

  98. function setCookie( name, value, expires, path, domain, secure ) {
  99.     var today = new Date();
  100.     today.setTime( today.getTime() );
  101.     if ( expires ) {
  102.         expires = expires * 1000 * 60 * 60 * 24;
  103.     }
  104.     var expires_date = new Date( today.getTime() + (expires) );
  105.     document.cookie = name+"="+escape( value ) +
  106.         ( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
  107.         ( ( path ) ? ";path=" + path : "" ) +
  108.         ( ( domain ) ? ";domain=" + domain : "" ) +
  109.         ( ( secure ) ? ";secure" : "" );
  110. }
  111.    
  112. function deleteCookie( name, path, domain ) {
  113.     if ( getCookie( name ) ) document.cookie = name + "=" +
  114.             ( ( path ) ? ";path=" + path : "") +
  115.             ( ( domain ) ? ";domain=" + domain : "" ) +
  116.             ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
  117. }
  118. -->
  119. </script>
  120. <script src="http://www.google-analytics.com/urchin.js&qu ... /javascript">
  121. </script>
  122. <script type="text/javascript">
  123. _uacct = "UA-97125-1";
  124. urchinTracker();
  125. </script>
  126. </head>
  127. <body onload="onlinks();">
  128. <div id="main">
  129.   <div id="title">自定义打开链接的方式</div>
  130.   <div id="property">
  131.     <label>
  132.     <input type="checkbox" id="c1" value="checkbox" onclick="newlinks();" />
  133.     打开链接为新窗口</label>
  134.     <span id="cookies_p" style="display:none">
  135.     <label>
  136.     <input type="checkbox" id="c2" value="checkbox" onclick="dolinks();" />
  137.     记住我的选择</label></span>
  138.   </div>
  139.   <div id="link">
  140.     <p><a href="http://www.163.com"> 网易</a></p>
  141.     <p><a href="http://www.sina.com.cn"> 新浪</a></p>
  142.     <p><a href="http://www.microsoft.com/china"> 微软中国</a></p>
  143.     <p><a href="http://www.blueidea.com"> 蓝色理想</a></p>
  144.   </div>
  145.   <div id="copyright">
  146. 最近由于要做一个新产品网站,涉及到W3C标准以及自定义等方面的内容,所以做了一个链接小实例,由于在XHTML1.0过渡型标准之前,target="_blank"属性一直是点击链接后弹出新窗口的方法,但是XHTML1.0严格型标准以及XHTML1.1之后就不再支持target属性,也就是说不能再用target="_blank"来弹出一个新的窗口!本实例探讨的就是在XHTML1.1标准下弹出新窗口的方法,并结合Cookies实现客户端自定义弹出新窗口的方法。 </div>
  147. <p class="center">
  148. <a href="http://validator.w3.org/check?uri=referer"><imgsrc="http://www.w3.org/Icons/valid-xhtml11&quo ... 31"width="88"/></a></p></div>
  149. </body>
  150. </html>
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注-册

本版积分规则

Archiver|手机版|小黑屋|DoDVip ( 桂ICP备14000730号 )

GMT+8, 2025-5-1 17:54 , Processed in 0.062879 second(s), 21 queries .

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表