您好,欢迎来到网站制作教程网!
css教程 css布局 web标准 浏览器兼容
access基础 mysql基础 sql server基础教程 oracle基础教程
fireworks教程 Dreamweaver教程 Flash教程 网页设计原理 网页欣赏
ASP基础教程 asp.net基础教程 html教程 xhtml教程 ajax教程 xml入门教程 正规则表达式
seo基础教程 策划盈利教程 建站基础
链接特效 导航特效 文本特效 日期时间特效 状态栏特效
javascript教程 vbscript教程
php与Smarty教程 php基础教程 php与数组教程 php与字符串教程 php与数据库
Web服务器教程 ftp服务器教程
ps制作网页元素 ps调色 ps抠图 ps照片美容 photoshop合成 ps文字效果 ps基础与技巧 coreldraw基础 ai基础 平面设计理论
当前位置: 网站制作教程 > css教程 > web标准 >

浅谈网页中自定义打开链接的方式

时间:2011-03-31 10:37来源:未知 作者:admin 点击:

浅谈网页中自定义打开链接的方式

由于在XHTML1.0过渡型标准之前,target="_blank"属性一直是点击链接后弹出新窗口的方法,但是XHTML1.0严格型标准以及XHTML1.1之后就不再支持target属性,

  也就是说不能再用target="_blank"来弹出一个新的窗口!本实例探讨的是在XHTML1.1标准下弹出新窗口的方法,并结合Cookies实现客户端自定义弹出新窗口的方法。
 

以下是引用片段:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">


<head>


<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />


<title>自定义打开链接的方式</title>


<style type="text/css">


<!--


#main {


    width: 600px;


    margin-right: auto;


    margin-left: auto;


    border: 1px solid #006699;


    height: 600px;


}


#title {


    background-color: #006699;


    padding: 10px;


    width: 100%;


    font-family: Verdana, Arial;


    font-size: 16px;


    color: #FFFFFF;


    font-weight: bold;


}


#property {


    margin: 10px;


    border: 1px solid #99CC00;


    height: 24px;


    background-color: #F8FEE9;


    font-size: 12px;


    line-height: 24px;


}


#link {


    margin: 10px;


    border: 1px solid #0099CC;


    background-color: #EFEEFF;


    font-size: 12px;


    height: 366px;


    padding: 20px;


}


#copyright {


    font-size: 12px;


    padding: 20px;


    color: #003333;


    line-height: 16px;


}


img{


    border-top-style: none;


    border-right-style: none;


    border-bottom-style: none;


    border-left-style: none;


}


.center {


    text-align: center;


}


-->


</style>


<script type="text/javascript">


<!--


function onlinks(){




if(getCookie("links")=="_blank"){


document.getElementById("c1").checked=true;


document.getElementById("c2").checked=true;


newlinks();//dolinks();


}


else{


document.getElementById("c1").checked=false;


document.getElementById("c2").checked=false;


newlinks();//dolinks();


}


}




function dolinks(){


document.getElementById("c2").checked?setCookie('links','_blank',1):deleteCookie('links');


}




function newlinks() { 


 document.getElementById("c1").checked?fc1():fc2()


 function fc1(){


 linktarget="_blank";document.getElementById("cookies_p").style.display="block"}


 function fc2(){


 linktarget="";document.getElementById("cookies_p").style.display="none"}




 var anchors = document.getElementsByTagName("a"); 


 for (var i=0; i<anchors.length; i++) { 


   var anchor = anchors[i]; 


   if (anchor.getAttribute("href")) 


     anchor.target = linktarget; 


 } 





function getCookie(name) {


    var start = document.cookie.indexOf( name + "=" );


    var len = start + name.length + 1;


    if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {


        return null;


    }


    if ( start == -1 ) return null;


    var end = document.cookie.indexOf( ";", len );


    if ( end == -1 ) end = document.cookie.length;


    return unescape( document.cookie.substring( len, end ) );


}    




function setCookie( name, value, expires, path, domain, secure ) {


    var today = new Date();


    today.setTime( today.getTime() );


    if ( expires ) {


        expires = expires * 1000 * 60 * 60 * 24;


    }


    var expires_date = new Date( today.getTime() + (expires) );


    document.cookie = name+"="+escape( value ) +


        ( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()


        ( ( path ) ? ";path=" + path : "" ) +


        ( ( domain ) ? ";domain=" + domain : "" ) +


        ( ( secure ) ? ";secure" : "" );


}


    


function deleteCookie( name, path, domain ) {


    if ( getCookie( name ) ) document.cookie = name + "=" +


            ( ( path ) ? ";path=" + path : "") +


            ( ( domain ) ? ";domain=" + domain : "" ) +


            ";expires=Thu, 01-Jan-1970 00:00:01 GMT";


}


-->


</script>


<script src="http://www.aaa.com/... /javascript"> 


</script>


<script type="text/javascript">


_uacct = "UA-97125-1";


urchinTracker();


</script>


</head>


<body onload="onlinks();">


<div id="main">


  <div id="title">自定义打开链接的方式</div>


  <div id="property">


    <label>


    <input type="checkbox" id="c1" value="checkbox" onclick="newlinks();" />


    打开链接为新窗口</label>


    <span id="cookies_p" style="display:none">


    <label>


    <input type="checkbox" id="c2" value="checkbox" onclick="dolinks();" />


    记住我的选择</label></span>


  </div>


  <div id="link">


    <p><a href="http://www.dushuwu.net"> 11</a></p>


        <p><a href="http://www.dushuwu.net"> 11</a></p>


    <p><a href="http://www.dushuwu.net"> 11</a></p>


        <p><a href="http://www.dushuwu.net"> 11</a></p>


  </div>


  <div id="copyright">


内容</div>


<p class="center">


<a href="http://validator.w3.org/check?uri=referer"><imgsrc="http://www.w3.org/Icons/valid-xhtml11&quo ... 31"width="88"/></a></p></div> 


</body>


</html>


Tags:
网站制作教程
责任编辑:admin

发表评论

请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
验证码:点击我更换图片

最新评论

  • 网络营销
  • 服务器
  • 数据库