JavaScript学习笔记(二)——从简单开始学起

学习新知识,探究新方法,是一个由"知器"——》"格物"——》"明理"——》"成道"依次演变的进化过程,需要循序渐进、由浅入深, 不过最终是否可以修炼成道骨仙风的"佛"家境界,则全凭个人的恒心、悟性和造化

百度和谷歌两大搜索引擎主页,表面看似简单,但内深藏玄机,其页面上的每一个像素点,用价值千金来形容,似乎也不为过。好了,下面我们用数据与事实说话,请看简单统计后的表格数据:

当用户打开网页,需要下载的流量(注:有的主页logo直接url地址加载,不在下面统计数据中)

另一种统计方法:用mht单文件保存,并结合logo大小统计(此方法没有上表准确)

数据与事实证明,行业只有做到了极致,才有发展前途。曾经独领风骚的雅虎,如今在市场的优胜劣汰中渐渐褪色——数风流人物,还看今朝

小知识:百度与谷歌的搜索质量,历来争论不休,介绍一款Baidu和Google的杂交搜索工具:百Google度 http://www.baigoogledu.com/

=================================================================================

上面统计表格,纯属个人好奇。现在言归正传,上一篇已简单介绍了学习JavaScript的工具,现在我们再进一步来"知器",知简单而又经典之神器——剖析并学习百度和谷歌两大搜索引擎的主页(由于它们的兼容性、容错性、简洁性,不愧为刚学习JavaScript的经典之作)

由于我们主要学习JavaScript,在此对HTML、CSS、DIV等知识不做过多的介绍,不过在学习使用它们需考虑到各种浏览器的兼容 (例如:中华人民共和国中国人民银行教育部等政府官方主页的兼容性就做得不够好,感兴趣者不妨利用非IE内核的浏览器如Firefox、Opera、Chrome查看之)

百度和Google,分别代表国内与全球最流行、最优秀的搜索引擎,其主页有专业的团队不断进行测试与维护,因此我们重点分析并探究这两款主页

1、百度主页源码

网址:http://www.baidu.com/

环境:Maxthon浏览器 已登录(sunboy_2050)账户

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4.     <meta http-equiv="Content-Type" content="text/html;charset=gb2312"> 
  5.     <title>百度一下,你就知道 </title> 
  6.     <mce:style><!-- 
  7.         body 
  8.         { 
  9.             font: 12px arial; 
  10.             text-align: center; 
  11.             background: #fff; 
  12.         } 
  13.         body, p, form 
  14.         { 
  15.             margin: 0; 
  16.             padding: 0; 
  17.         } 
  18.         body, form, #lg 
  19.         { 
  20.             position: relative; 
  21.         } 
  22.         td 
  23.         { 
  24.             text-align: left; 
  25.         } 
  26.         img 
  27.         { 
  28.             border: 0; 
  29.         } 
  30.         a 
  31.         { 
  32.             color: #00c; 
  33.         } 
  34.         a:active 
  35.         { 
  36.             color: #f60; 
  37.         } 
  38.         #u 
  39.         { 
  40.             padding: 7px 10px 3px 0; 
  41.             text-align: right; 
  42.         } 
  43.         #m 
  44.         { 
  45.             width: 650px; 
  46.             margin: 0 auto; 
  47.         } 
  48.         #nv 
  49.         { 
  50.             font-size: 16px; 
  51.             margin: 0 0 4px -32px; 
  52.         } 
  53.         #nv a, #nv b, #su, #lk 
  54.         { 
  55.             font-size: 14px; 
  56.         } 
  57.         #lg 
  58.         { 
  59.             margin: -17px 0 9px; 
  60.         } 
  61.         #fm 
  62.         { 
  63.             padding-left: 111px; 
  64.             text-align: left; 
  65.         } 
  66.         #kw 
  67.         { 
  68.             width: 391px; 
  69.             line-height: 16px; 
  70.             padding: 3px 1px; 
  71.             margin: 0 6px 0 0; 
  72.             font: 16px arial; 
  73.         } 
  74.         #su 
  75.         { 
  76.             width: 78px; 
  77.             height: 28px; 
  78.             line-height: 24px; 
  79.         } 
  80.         #kw, #su 
  81.         { 
  82.             vertical-align: middle; 
  83.         } 
  84.         #lk 
  85.         { 
  86.             margin: 33px 0; 
  87.         } 
  88.         #lk span 
  89.         { 
  90.             font: 14px "宋体"; 
  91.         } 
  92.         #lm 
  93.         { 
  94.             height: 60px; 
  95.         } 
  96.         #lh 
  97.         { 
  98.             margin: 16px 0 5px; 
  99.             font: 12px "宋体"; 
  100.         } 
  101.         #lh a 
  102.         { 
  103.             font: 12px arial; 
  104.         } 
  105.         #hp 
  106.         { 
  107.             position: absolute; 
  108.             line-height: 14px; 
  109.             margin: 0 0 0 6px; 
  110.             top: -1px; *top:2px} 
  111.         #cp, #cp a 
  112.         { 
  113.             color: #77c; 
  114.         } 
  115.         #sx 
  116.         { 
  117.             color: #00C; 
  118.             text-decoration: underline; 
  119.             cursor: pointer; 
  120.         } 
  121.      
  122. --></mce:style><style mce_bogus="1">        body 
  123.         { 
  124.             font: 12px arial; 
  125.             text-align: center; 
  126.             background: #fff; 
  127.         } 
  128.         body, p, form 
  129.         { 
  130.             margin: 0; 
  131.             padding: 0; 
  132.         } 
  133.         body, form, #lg 
  134.         { 
  135.             position: relative; 
  136.         } 
  137.         td 
  138.         { 
  139.             text-align: left; 
  140.         } 
  141.         img 
  142.         { 
  143.             border: 0; 
  144.         } 
  145.         a 
  146.         { 
  147.             color: #00c; 
  148.         } 
  149.         a:active 
  150.         { 
  151.             color: #f60; 
  152.         } 
  153.         #u 
  154.         { 
  155.             padding: 7px 10px 3px 0; 
  156.             text-align: right; 
  157.         } 
  158.         #m 
  159.         { 
  160.             width: 650px; 
  161.             margin: 0 auto; 
  162.         } 
  163.         #nv 
  164.         { 
  165.             font-size: 16px; 
  166.             margin: 0 0 4px -32px; 
  167.         } 
  168.         #nv a, #nv b, #su, #lk 
  169.         { 
  170.             font-size: 14px; 
  171.         } 
  172.         #lg 
  173.         { 
  174.             margin: -17px 0 9px; 
  175.         } 
  176.         #fm 
  177.         { 
  178.             padding-left: 111px; 
  179.             text-align: left; 
  180.         } 
  181.         #kw 
  182.         { 
  183.             width: 391px; 
  184.             line-height: 16px; 
  185.             padding: 3px 1px; 
  186.             margin: 0 6px 0 0; 
  187.             font: 16px arial; 
  188.         } 
  189.         #su 
  190.         { 
  191.             width: 78px; 
  192.             height: 28px; 
  193.             line-height: 24px; 
  194.         } 
  195.         #kw, #su 
  196.         { 
  197.             vertical-align: middle; 
  198.         } 
  199.         #lk 
  200.         { 
  201.             margin: 33px 0; 
  202.         } 
  203.         #lk span 
  204.         { 
  205.             font: 14px "宋体"; 
  206.         } 
  207.         #lm 
  208.         { 
  209.             height: 60px; 
  210.         } 
  211.         #lh 
  212.         { 
  213.             margin: 16px 0 5px; 
  214.             font: 12px "宋体"; 
  215.         } 
  216.         #lh a 
  217.         { 
  218.             font: 12px arial; 
  219.         } 
  220.         #hp 
  221.         { 
  222.             position: absolute; 
  223.             line-height: 14px; 
  224.             margin: 0 0 0 6px; 
  225.             top: -1px; *top:2px} 
  226.         #cp, #cp a 
  227.         { 
  228.             color: #77c; 
  229.         } 
  230.         #sx 
  231.         { 
  232.             color: #00C; 
  233.             text-decoration: underline; 
  234.             cursor: pointer; 
  235.         } 
  236.     </style> 
  237. </head> 
  238. <body> 
  239.     <p id="u"> 
  240.         <a href="http://passport.baidu.com" mce_href="http://passport.baidu.com" target="_blank"><b>sunboy_2050</b> </a>  
  241.         |  <a href="http://my.baidu.com/index.html?my=1&tn=baidu_myps_pg" mce_href="http://my.baidu.com/index.html?my=1&tn=baidu_myps_pg">我在百度 </a> 
  242.           | <a href="http://hi.baidu.com/sys/checkuser/sunboy_2050/3" mce_href="http://hi.baidu.com/sys/checkuser/sunboy_2050/3" target="_blank">我的空间</a>  
  243.         | <a href="http://passport.baidu.com/?logout&tpl=mn&bdstoken=2378cd7fe769741d2d5f1d86bfd368ff" mce_href="http://passport.baidu.com/?logout&tpl=mn&bdstoken=2378cd7fe769741d2d5f1d86bfd368ff">退出 
  244.         </a> 
  245.     </p> 
  246.     <div id="m"> 
  247.         <p id="lg"> 
  248.             <img src="http://www.baidu.com/img/baidu_logo.gif" mce_src="http://www.baidu.com/img/baidu_logo.gif" width="270" height="129" usemap="#mp"></p> 
  249.         <p id="nv"> 
  250.             <a href="http://news.baidu.com" mce_href="http://news.baidu.com">新 闻</a> <b>网 页</b> <a href="http://tieba.baidu.com" mce_href="http://tieba.baidu.com"> 
  251.                 贴 吧</a> <a href="http://zhidao.baidu.com" mce_href="http://zhidao.baidu.com">知 道</a> <a href="http://mp3.baidu.com" mce_href="http://mp3.baidu.com"> 
  252.                     MP3</a> <a href="http://image.baidu.com" mce_href="http://image.baidu.com">图 片</a> <a href="http://video.baidu.com" mce_href="http://video.baidu.com"> 
  253.                         视 频</a> <a href="http://map.baidu.com" mce_href="http://map.baidu.com">地 图</a></p> 
  254.         <div id="fm"> 
  255.             <form name="f" action="s"> 
  256.             <input type="text" name="wd" id="kw" maxlength="100"> 
  257.             <input type="hidden" name="ch" value=""> 
  258.             <input type="hidden" name="tn" value="maxthon2"> 
  259.             <input type="hidden" name="bar" value=""> 
  260.             <input type="submit" value="百度一下" id="su"> 
  261.             <span id="hp"><a href="/gaoji/preferences.html" mce_href="gaoji/preferences.html">设置</a><br> 
  262.                 <span id="sx">手写</span> </span> 
  263.             </form> 
  264.         </div> 
  265.         <p id="lk"> 
  266.             <a href="http://hi.baidu.com" mce_href="http://hi.baidu.com">空间</a> <a href="http://baike.baidu.com" mce_href="http://baike.baidu.com">百科</a> <a href="http://www.hao123.com" mce_href="http://www.hao123.com"> 
  267.                 hao123</a><span> | <a href="/more/" mce_href="more/">更多>></a></span></p> 
  268.         <p id="lm"> 
  269.         </p> 
  270.         <p> 
  271.             <a id="st" onclick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.baidu.com')" 
  272.                 href="http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com" mce_href="http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com">把百度设为主页</a></p> 
  273.         <p id="lh"> 
  274.             <a href="http://e.baidu.com/?refer=888" mce_href="http://e.baidu.com/?refer=888">加入百度推广</a> | <a href="http://top.baidu.com" mce_href="http://top.baidu.com"> 
  275.                 搜索风云榜</a> | <a href="http://home.baidu.com" mce_href="http://home.baidu.com">关于百度</a> | <a href="http://ir.baidu.com" mce_href="http://ir.baidu.com"> 
  276.                     About Baidu</a></p> 
  277.         <p id="cp"> 
  278.             ©2010 Baidu <a href="/duty/" mce_href="duty/">使用百度前必读</a> <a href="http://www.miibeian.gov.cn" mce_href="http://www.miibeian.gov.cn" 
  279.                 target="_blank">京ICP证030173号</a> 
  280.             <img src="http://gimg.baidu.com/img/gs.gif" mce_src="http://gimg.baidu.com/img/gs.gif"></p> 
  281.     </div> 
  282.     <map name="mp"> 
  283.         <area shape="rect" mce_shape="rect" coords="43,22,227,91" mce_coords="43,22,227,91" href="http://hi.baidu.com/baidu/" mce_href="http://hi.baidu.com/baidu/" target="_blank" 
  284.             title="点此进入 百度的空间"> 
  285.     </map> 
  286. </body> 
  287.  
  288. <mce:script type="text/javascript"><!-- 
  289.     var w = window, d = document, n = navigator, k = d.f.wd, 
  290.     a = d.getElementById("nv").getElementsByTagName("a"); 
  291.     if (n.userAgent.indexOf("MSIE") == -1 || window.opera) { 
  292.         d.getElementById("st").style.display = "none" 
  293.     }; 
  294.     for (var i = 0; i < a.length; i++) { 
  295.         a[i].onclick = function() { 
  296.             if (k.value.length > 0) { 
  297.                 var o = this, h = o.href, q = encodeURIComponent(k.value); 
  298.                 if (h.indexOf("q=") != -1) { 
  299.                     o.href = h.replace(/q=[^&$]*/, "q=" + q) 
  300.                 } 
  301.                 else { 
  302.                     this.href += "?q=" + q 
  303.                 } 
  304.             } 
  305.         } 
  306.     }; 
  307.     (function() { 
  308.         if (/q=([^&]+)/.test(location.search)) { 
  309.             k.value = decodeURIComponent(RegExp.$1) 
  310.         } 
  311.     })(); 
  312.     if (n.cookieEnabled && !/sug?=0/.test(d.cookie)) { 
  313.         d.write('<script src="http://www.baidu.com/js/bdsug.js?v=1.0.3.0" mce_src="http://www.baidu.com/js/bdsug.js?v=1.0.3.0"><//script>') 
  314.     }; 
  315.     if (w.attachEvent) { 
  316.         w.attachEvent("onload", function() { 
  317.             k.focus(); 
  318.         }) 
  319.     } 
  320.     else { 
  321.         w.addEventListener('load', function() { 
  322.             k.focus() 
  323.         }, true) 
  324.     }; 
  325.     w.onunload = function() { }; 
  326.     var hw = {}; hw.i = d.getElementById("sx"); 
  327.     var il = false
  328.     if (/msie (/d+/./d)/i.test(n.userAgent)) { 
  329.         hw.i.setAttribute("unselectable", "on") 
  330.     } 
  331.     else { 
  332.         var sL = k.value.length; k.selectionStart = sL; k.selectionEnd = sL 
  333.     } 
  334.     hw.i.onclick = function(B) { 
  335.         var B = B || w.event; B.stopPropagation ? B.stopPropagation() : (B.cancelBubble = true); 
  336.         if (d.selection && d.activeElement.id && d.activeElement.id == "kw") { 
  337.             hw.hasF = 1 
  338.         } 
  339.         else { 
  340.             if (!d.selection) { 
  341.                 hw.hasF = 1 
  342.             } 
  343.         } if (!il) { 
  344.             var A = d.createElement("script"); 
  345.             A.setAttribute("src", "http://www.baidu.com/hw/hwInput.js"); 
  346.             d.getElementsByTagName("head")[0].appendChild(A); il = true
  347.             setTimeout( 
  348.             function() { 
  349.                 if (baidu) { 
  350.                     baidu.sug.initial() 
  351.                 } 
  352.             } 
  353.             , 1000) 
  354.         } 
  355.     }; 
  356. // --></mce:script> 
  357.  
  358. </html> 
  359. <!--f99b4c28737a54f7--> 

=================================================================================

剖析1——设置主页

<a id="st" οnclick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.baidu.com')"
                href="
http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com">把百度设为主页</a>

上面代码中,利用this.style.behavior='url()'; 和 this.setHomePage() 通过onclick用户单击事件,设置主页。但此方法仅限IE内核浏览器

=================================================================================

剖析2——解决设置主页仅限IE内核问题

var w = window, d = document, n = navigator, k = d.f.wd, a = d.getElementById("nv").getElementsByTagName("a");
    if (n.userAgent.indexOf("MSIE") == -1 || window.opera) {
        d.getElementById("st").style.display = "none"
    };

上面代码中,if语句提取navigator浏览器的信息是否为非IE内核浏览器(MSIE-Microsoft IE)或为opera浏览器,若二者之一成立,则<a id="st" ...>...</a>此信息栏不显示。此外,经过我认真测试,利用Opera、Firefox、Chrome等非IE内核浏览器成功登陆账户后,设置主页栏确为不显示

 

=================================================================================

 

剖析3——map图片映射

<img src="http://www.baidu.com/img/baidu_logo.gif" width="270" height="129" usemap="#mp"></p>

<map name="mp">
        <area shape="rect" coords="43,22,227,91" href="
http://hi.baidu.com/baidu/" target="_blank"
            title="点此进入 百度的空间">
</map>

上面代码中,map作为img的客户端图片映射,利用usemap="#mp" 关联起来,设置矩形的shape="rect"热点区域coords="43,22,227,91" 当点击超链接后跳转href="http://hi.baidu.com/baidu/"

 

=================================================================================

 

剖析4——人性化的onclick()事件设计

var w = window, d = document, n = navigator, k = d.f.wd, a = d.getElementById("nv").getElementsByTagName("a");

for (var i = 0; i < a.length; i++) {
        a[i].onclick = function() {
            if (k.value.length > 0) {
                var o = this, h = o.href, q = encodeURIComponent(k.value);
                if (h.indexOf("q=") != -1) {
                    o.href = h.replace(/q=[^&$]*/, "q=" + q)
                }
                else {
                    this.href += "?q=" + q
                }
            }
        }
    };

上面代码中,通过document对象的getElementById()方法,获得nv对象后,再调用getElementsByTagName获得<a>标签对象集合a

接着,我们仔细分析if (h.indexOf("q=") != -1) 这段代码的功能,或许你会发现百度在这儿对搜索做得很人性化,详细分析如下:

首先,通过字符编码函数q = encodeURIComponent() 转义搜索框k中的value信息

然后,通过if(h.indexOf("q=") != -1) 判断链接字符串中是否包含"q="信息,if包含,则利用q=[^&$]* 正则表达式,对其进行&替换(^起始符,$结束符,学习正则表示式,请看我先前的博客正则表达式的学习与小结);else不包含,则执行this.href += "?q=" + q直接添加"?q="子字符串

示例:我们在搜索输入框键入"百度",并点击搜索,得到url:http://www.baidu.com/s?wd=%B0%D9%B6%C8 然后再换做搜索"google"得到url:http://www.baidu.com/s?bs=%B0%D9%B6%C8&f=8&wd=google 仔细分析链接字符串可知第一个字符均含? 、待搜索关键词前均含wd=()(name)、以及replace后的&(q=),其中%B0%D9%B6%C8是汉字"百度"的双字节编码,google英文不做编码转换

最后,人性化设计亮点出现了,通过for(;i<a.length;)循环,让我们输入了搜索信息后,点击其它的链接,如新闻、知道等,直接跳转并检索的信息,正好是在刚才页面输入框中的信息,这样就不需用户重复输入搜索关键字了,简单、快捷

评价:这个小细节的优化,如果用户不太注意,是感觉不出来的。大家不妨试试Google,先在web页面搜索框中输入搜索关键词"google",现在我突然不想检索"google"的web信息,而是想检索"Google"图片信息,于是我们直接点击左上角的Images链接,页面的确也跳转到Images页面,但并未显示检索结果,需要用户重新再点击一下search按钮,然后才进行检索、显示结果的

 

=================================================================================

剖析5——函数(function(){}) ()格式的疑惑

(function() {
        if (/q=([^&]+)/.test(location.search)) {
            k.value = decodeURIComponent(RegExp.$1)
        }
    })();

在上述代码中,看到了函数function()格式,感觉怪怪的,一来没有函数名,二来函数体还被小括号(function(){})包起来,我刚学JavaScript,对这种函数格式还不懂(感觉是无需调用,直接执行函数体),具体用法还希望有高人能帮我指点一下,给出确切答案

下面我对JavaScript语法,做简单分析:([^&]+)/.test(location.search) 是对正则表达式([^&]+) 进行test()模式匹配,location.search 是调用location对象的search属性,提取url中?后的子字符串,如http://www.baidu.com/s?wd=%B0%D9%B6%C8 则提取?wd=%B0%D9%B6%C8 子字符串,此句if测试location.search字符串中是否含有匹配以&字符开头的字串;如果有,则对正则表达式的第一列RegExp.$1 进行反译码(decodeURIComponent

评析:对这段代码,以我目前三脚猫的功夫,还没研究明白透彻,希望有武林高手多帮我指点指点,这样我也进步得更快一些,好修炼内功

  =================================================================================

剖析6——Navigator的cookie用法

if (n.cookieEnabled && !/sug?=0/.test(d.cookie)) {
        d.write('<script src=http://www.baidu.com/js/bdsug.js?v=1.0.3.0><//script>')
    };

在上述代码中,n.cookieEnabled 是判断浏览器navigator  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值