javascript SocialHistory 檢查訪問(wèn)者是否訪問(wèn)過(guò)某站點(diǎn)
更新時(shí)間:2008年08月02日 01:11:55 作者:
今天delicious上這個(gè)名為 SocialHistory 的腳本十分引人注目。源代碼可以在這里下載。這段js代碼的功能就是判斷你的用戶有沒(méi)有訪問(wèn)過(guò)某個(gè)網(wǎng)站。使用方法很簡(jiǎn)單,例如:
| window.onload = function() { var sl = new SocialHistory(); alert(sl.doesVisit("www.dhdzp.com")); } 如果用戶曾經(jīng)使用過(guò)www.dhdzp.com,那么該函數(shù)就會(huì)返回真,否則返回假。 其實(shí)原理并不復(fù)雜,它利用了鏈接的 a:visited 偽類的屬性。首先在頁(yè)面上生成一個(gè)iframe,并在這個(gè)iframe中設(shè)置 a 和 a:visited 為不同的樣式。然后將網(wǎng)站的鏈接插入到 iframe 中。瀏覽器就會(huì)根據(jù)用戶的訪問(wèn)歷史,為訪問(wèn)過(guò)的鏈接設(shè)置 a:visited 的樣式。最后再獲得鏈接的最終樣式,如果是 a:visited,就可以認(rèn)為用戶訪問(wèn)過(guò)該網(wǎng)站了。具體的實(shí)現(xiàn)方式可以參考源代碼。 這個(gè)腳本主要用于顯示社會(huì)性書(shū)簽的圖標(biāo),可以恰到好處地顯示用戶所使用的網(wǎng)站。但我擔(dān)心,這樣的做法是不是有盜取用戶隱私之嫌?雖然這個(gè)方法只能判斷用戶有無(wú)訪問(wèn)特定的網(wǎng)站,并不能無(wú)限制地得到所有訪問(wèn)歷史。 /* |
| * Social Limit - Only the social you care about. |
| * |
| * Enables your site to know which social bookmarking badges to display to your |
| * visitors. It tells you all social sites the user has gone to, or you can |
| * query for a specific one. |
| * |
| * For example: |
| * |
| * var sl = SocialHistory(); |
| * alert( sl.doesVisit("Digg") ); // Returns true/false, -1 if unknown. |
| * var listOfVisitedSites = sl.visitedSites(); |
| * var checkedSites = sl.checkedSites(); |
| * |
| * If you want to add more sites to check, you can pass that in as a dictionary |
| * to History: |
| * |
| * var more = { "Humanized": "http://humanized.com", |
| * "Azarask.in": ["http://azarask.in", "http://azarask.in/blog"] |
| * }; |
| * var sl = SocialHistory(more); |
| * alert( sl.doesVisit("Humanized") ); |
| * |
| * For a list of built-in sites, see the sites variable below. |
| * |
| * Copyright (c) 2008 Aza Raskin (http://azarask.in/blog) |
| * |
| * Permission is hereby granted, free of charge, to any person obtaining a copy |
| * of this software and associated documentation files (the "Software"), to deal |
| * in the Software without restriction, including without limitation the rights |
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| * copies of the Software, and to permit persons to whom the Software is |
| * furnished to do so, subject to the following conditions: |
| * |
| * The above copyright notice and this permission notice shall be included in |
| * all copies or substantial portions of the Software. |
| * |
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| * THE SOFTWARE. |
| * |
| */ |
| var SocialHistory = function( moreSites ){ |
| var sites = { |
| "Digg": ["http://digg.com", "http://digg.com/login"], |
| "Reddit": ["http://reddit.com", "http://reddit.com/new/", "http://reddit.com/controversial/", "http://reddit.com/top/", "http://reddit.com/r/reddit.com/", "http://reddit.com/r/programming/"], |
| "StumbleUpon": ["http://stumbleupon.com"], |
| "Yahoo Buzz": ["http://buzz.yahoo.com"], |
| "Facebook": ["http://facebook.com/home.php", "http://facebook.com", "https://login.facebook.com/login.php"], |
| "Del.icio.us": ["https://secure.del.icio.us/login", "http://del.icio.us/"], |
| "MySpace": ["http://www.myspace.com/"], |
| "Technorati": ["http://www.technorati.com"], |
| "Newsvine": ["https://www.newsvine.com", "https://www.newsvine.com/_tools/user/login"], |
| "Songza": ["http://songza.com"], |
| "Slashdot": ["http://slashdot.org/"], |
| "Ma.gnolia": ["http://ma.gnolia.com/"], |
| "Blinklist": ["http://www.blinklist.com"], |
| "Furl": ["http://furl.net", "http://furl.net/members/login"], |
| "Mister Wong": ["http://www.mister-wong.com"], |
| "Current": ["http://current.com", "http://current.com/login.html"], |
| "Menaeme": ["http://meneame.net", "http://meneame.net/login.php"], |
| "Oknotizie": ["http://oknotizie.alice.it", "http://oknotizie.alice.it/login.html.php"], |
| "Diigo": ["http://www.diigo.com/", "https://secure.diigo.com/sign-in"], |
| "Funp": ["http://funp.com", "http://funp.com/account/loginpage.php"], |
| "Blogmarks": ["http://blogmarks.net"], |
| "Yahoo Bookmarks": ["http://bookmarks.yahoo.com"], |
| "Xanga": ["http://xanga.com"], |
| "Blogger": ["http://blogger.com"], |
| "Last.fm": ["http://www.last.fm/", "https://www.last.fm/login/"], |
| "N4G": ["http://www.n4g.com"], |
| "Faves": ["http://faves.com", "http://faves.com/home", "https://secure.faves.com/signIn"], |
| "Simpy": ["http://www.simpy.com", "http://www.simpy.com/login"], |
| "Yigg": ["http://www.yigg.de"], |
| "Kirtsy": ["http://www.kirtsy.com", "http://www.kirtsy.com/login.php"], |
| "Fark": ["http://www.fark.com", "http://cgi.fark.com/cgi/fark/users.pl?self=1"], |
| "Mixx": ["https://www.mixx.com/login/dual", "http://www.mixx.com"], |
| "Google Bookmarks": ["http://www.google.com/bookmarks", "http://www.google.com/ig/add?moduleurl=bookmarks.xml&hl=en"], |
| "Subbmitt": ["http://subbmitt.com/"] |
| }; |
| for( var site in moreSites ) { |
| // If we don't have the site, create the URL list. |
| if( typeof( sites[site] ) == "undefined" ) sites[site] = []; |
| // If the value is string, just push that onto the URL list. |
| if( typeof( moreSites[site] ) == "string" ) |
| sites[site].push( moreSites[site] ); |
| else |
| sites[site] = sites[site].concat( moreSites[site] ); |
| } |
| var visited = {}; |
| function getStyle(el, scopeDoc,styleProp) { |
| if (el.currentStyle) |
| var y = el.currentStyle[styleProp]; |
| else if (window.getComputedStyle) |
| var y = scopeDoc.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp); |
| return y; |
| } |
| function remove( el ) { |
| el.parentNode.removeChild( el ); |
| } |
| // Code inspired by: |
| // bindzus.wordpress.com/2007/12/24/adding-dynamic-contents-to-iframes |
| function createIframe() { |
| var iframe = document.createElement("iframe"); |
| iframe.style.position = "absolute"; |
| iframe.style.visibility = "hidden"; |
| document.body.appendChild(iframe); |
| // Firefox, Opera |
| if(iframe.contentDocument) iframe.doc = iframe.contentDocument; |
| // Internet Explorer |
| else if(iframe.contentWindow) iframe.doc = iframe.contentWindow.document; |
| // Magic: Force creation of the body (which is null by default in IE). |
| // Also force the styles of visited/not-visted links. |
| iframe.doc.open(); |
| iframe.doc.write('<style>'); |
| iframe.doc.write("a{color: #000000; display:none;}"); |
| iframe.doc.write("a:visited {color: #FF0000; display:inline;}"); |
| iframe.doc.write('</style>'); |
| iframe.doc.close(); |
| // Return the iframe: iframe.doc contains the iframe. |
| return iframe; |
| } |
| var iframe = createIframe(); |
| function embedLinkInIframe( href, text ) { |
| var a = iframe.doc.createElement("a"); |
| a.href = href; |
| a.innerHTML = site; |
| iframe.doc.body.appendChild( a ); |
| } |
| for( var site in sites ) { |
| var urls = sites[site]; |
| for( var i=0; i<urls.length; i++ ) { |
| // You have to create elements in the scope of the iframe for IE. |
| embedLinkInIframe( urls[i], site ); |
| // Automatically try variations of the URLS with and without the "www" |
| if( urls[i].match(/www\./) ){ |
| var sansWWW = urls[i].replace(/www\./, ""); |
| embedLinkInIframe( sansWWW, site ); |
| } else { |
| // 2 = 1 for length of string + 1 for slice offset |
| var httpLen = urls[i].indexOf("http://") + 2; |
| var withWWW = urls[i].substring(0, httpLen ) + "www." + urls[i].substring( httpLen ); |
| embedLinkInIframe( withWWW, site ); |
| } |
| } |
| } |
| var links = iframe.doc.body.childNodes; |
| for( var i=0; i<links.length; i++) { |
| // Handle both Firefox/Safari, and IE (respectively) |
| var displayValue = getStyle(links[i], iframe.doc, "display"); |
| var didVisit = displayValue != "none"; |
| if( didVisit ){ |
| visited[ links[i].innerHTML ] = true; |
| } |
| } |
| remove( iframe ); |
| return new (function(){ |
| var usedSites = []; |
| for( var site in visited ){ |
| usedSites.push( site ); |
| } |
| // Return an array of visited sites. |
| this.visitedSites = function() { |
| return usedSites; |
| } |
| // Return true/false. If we didn't check the site, return -1. |
| this.doesVisit = function( site ) { |
| if( typeof( sites[site] ) == "undefined" ) |
| return -1; |
| return typeof( visited[site] ) != "undefined"; |
| } |
| var checkedSites = []; |
| for( var site in sites ){ |
| checkedSites.push( site ); |
| } |
| // Return a list of the sites checked. |
| this.checkedSites = function(){ |
| return checkedSites; |
| } |
| })(); |
| } |
您可能感興趣的文章:
- JavaScript編程中window的location與history對(duì)象詳解
- javascript history對(duì)象(歷史記錄)使用方法(實(shí)現(xiàn)瀏覽器前進(jìn)后退)
- js history對(duì)象簡(jiǎn)單實(shí)現(xiàn)返回和前進(jìn)
- JavaScript入門教程(7) History歷史對(duì)象
- JavaScript中的History歷史對(duì)象
- JavaScript的History API使搜索引擎抓取AJAX內(nèi)容
- js判斷當(dāng)頁(yè)面無(wú)法回退時(shí)關(guān)閉網(wǎng)頁(yè)否則就history.go(-1)
- javascript:history.go()和History.back()的區(qū)別及應(yīng)用
- javascript history對(duì)象詳解
相關(guān)文章
JS中的.length屬性和.size()方法的區(qū)別
length是js原生屬性,size()是jQuery方法,如果你只是想獲取元素的個(gè)數(shù),兩者效果一樣既.length和.size()獲取的值是一樣的2024-08-08
uniapp中使用計(jì)時(shí)器setInterval的場(chǎng)景與方法
計(jì)時(shí)器在日常開(kāi)發(fā)中經(jīng)常會(huì)遇到,下面這篇文章主要給大家介紹了關(guān)于uniapp中使用計(jì)時(shí)器setInterval的場(chǎng)景與方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
實(shí)例詳解JSON數(shù)據(jù)格式及json格式數(shù)據(jù)域字符串相互轉(zhuǎn)換
JSON(JavaScript Object Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式。JSON采用完全獨(dú)立于語(yǔ)言的文本格式,這些特性使JSON成為理想的數(shù)據(jù)交換語(yǔ)言2016-01-01
javascript中setTimeout的問(wèn)題解決方法
這篇文章主要介紹了javascript中setTimeout的問(wèn)題以及對(duì)應(yīng)的解決方法,需要的朋友可以參考下2014-05-05
Javascript 實(shí)現(xiàn)的數(shù)獨(dú)解題算法網(wǎng)頁(yè)實(shí)例
此算法的實(shí)現(xiàn),就是模擬人腦的思考和計(jì)算過(guò)程,有需要的朋友可以參考一下2013-10-10
JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)播放器
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)播放器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
微信小程序返回到頂部功能的簡(jiǎn)單實(shí)現(xiàn)
在做微信小程序開(kāi)發(fā)時(shí),遇到一個(gè)問(wèn)題,要如何實(shí)現(xiàn)返回頂部的功能,下面這篇文章主要給大家介紹了微信小程序返回到頂部功能的簡(jiǎn)單實(shí)現(xiàn),文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
利用Javascript裁剪圖片并存儲(chǔ)的簡(jiǎn)單實(shí)現(xiàn)
裁剪圖片對(duì)我們來(lái)說(shuō)是再熟悉不過(guò)的了,最近工作中就又遇到了這個(gè)需求,所以想著干脆整理下來(lái),方法大家和自己在需要的時(shí)候參考學(xué)習(xí),所以這篇文章主要介紹了利用Javascript裁剪圖片并存儲(chǔ)的簡(jiǎn)單實(shí)現(xiàn),后端PHP處理我用的是THINKPHP框架,需要的朋友可以參考下。2017-03-03

