2012-04-23 8 views
7

मुझे सामान्य वेबपृष्ठ से फ़ेविकॉन का यूआरएल प्राप्त करने का एक तरीका चाहिए क्योंकि फ़ेविकॉन हमेशा बेस यूआरएल पर नहीं है।जावास्क्रिप्ट में एक सामान्य वेबपृष्ठ से फेविकॉन का यूआरएल कैसे प्राप्त करें?

पी। बाहरी सेवा या पुस्तकालय का उपयोग किए बिना।

+0

http://en.wikipedia.org/wiki/Favicon# पहुंच क्षमता पहले से ही कई संकेत प्रदान करती है। –

+0

@xRobot: यदि आप चाहते हैं कि लोग सिर्फ आपके लिए कोड लिखें, तो ऐसा करने के लिए वहां कई साइटें हैं। StackOverflow जगह नहीं है। –

उत्तर

8

यह काम करने के लिए लगता है:

var getFavicon = function(){ 
    var favicon = undefined; 
    var nodeList = document.getElementsByTagName("link"); 
    for (var i = 0; i < nodeList.length; i++) 
    { 
     if((nodeList[i].getAttribute("rel") == "icon")||(nodeList[i].getAttribute("rel") == "shortcut icon")) 
     { 
      favicon = nodeList[i].getAttribute("href"); 
     } 
    } 
    return favicon;   
} 

alert(getFavicon());​ 

या एक ऑनलाइन उदाहरण के लिए http://jsfiddle.net/PBpgY/3/ पर एक नजर है।

+0

धन्यवाद, क्या पूरा यूआरएल पाने के लिए कोई रास्ता नहीं है और न केवल/favicon.png? – xRobot

+0

उदाहरण के लिए यदि आप stackoverflow.com पर इसे आजमाते हैं, तो यह 'rel = "शॉर्टकट आइकन"' विशेषता के कारण काम नहीं करेगा। मेरा एक नज़र डालें – sp00m

+0

इसके साथ यह काम करता है: अगर ((document.getElementsByTagName ("link") [i] .getAttribute ("rel") == "icon") || (document.getElementsByTagName ("link") [i] .getAttribute ("रिलायंस") == "शॉर्टकट आइकन"))। लेकिन मुझे पूरा यूआरएल चाहिए और न केवल favicon.png :( – xRobot

5

फेविकॉन /favicon.ico पर है जब तक कि आपके पास <link rel="icon" href="..."> तत्व नहीं है। तो आप सभी link elementsdocument.getElementsByTagName के माध्यम से प्राप्त कर सकते हैं और फिर NodeList में प्रत्येक तत्व को देखने के लिए देख सकते हैं कि उनमें से कोई भी rel विशेषता "icon" के साथ विशेषता है और यदि ऐसा है, तो href देखें। (आप भी लोगों पर लग सकता है जहां rel"shortcut icon" या "icon shortcut" ऐतिहासिक कारणों के लिए है।)

+0

मैं rel = "शॉर्टकट आइकन" के साथ तत्व कैसे प्राप्त कर सकता हूं? – xRobot

+1

@xRobot: जैसा कि मैंने कहा था, आप 'नोडलिस्ट' के माध्यम से लूप करते हैं, आप 'getElementsByTagName' से वापस आ गए हैं, 'rel =" शॉर्टकट आइकन "' (या सिर्फ' 'आइकन' 'के साथ एक ढूंढ रहे हैं, क्योंकि यह नया रूप है) । 'नोडलिस्ट' को अनुक्रमित किया गया है, उदाहरण के लिए, यदि आपके पास 'var list = document.getElementsByTagName ("link") है;' तब 'सूची [0]' पहला मिलान तत्व है, 'सूची [1] 'दूसरा है , आदि। एक सुविधाजनक 'लंबाई' संपत्ति भी है। प्रदान किए गए लिंक देखें। –

14

लोगों के लिए अभी भी उपरोक्त कोड के साथ फेविकॉन नहीं मिल रहा है;

  1. अधिकतर ब्राउज़र एक अनुरोध (/favicon.ico) भेज कर फ़ेविकॉन हो रही खुद को, के बजाय html में समर्थन करते हैं।

  2. Google द्वारा एक और समाधान प्रदान किया जाता है।

    किसी डोमेन के लिए फ़ेविकॉन प्राप्त करने के लिए उपयोग करें:
    https://plus.google.com/_/favicon?domain=www.stackoverflow.com

    एक URL के लिए फ़ेविकॉन प्राप्त करने के लिए, का उपयोग करें:
    https://plus.google.com/_/favicon?domain_url=http://www.stackoverflow.com

2

लाइव काम कर बेला उदाहरण: http://jsfiddle.net/sc8qp/2/

बस रेगेक्स के बिना अच्छे उपाय और पूर्णता के लिए:

function getIcons() { 
    var links = document.getElementsByTagName('link'); 
    var icons = []; 

    for(var i = 0; i < links.length; i++) { 
     var link = links[i]; 

     //Technically it could be null/undefined if someone didn't set it! 
     //People do weird things when building pages! 
     var rel = link.getAttribute('rel'); 
     if(rel) { 
      //I don't know why people don't use indexOf more often 
      //It is faster than regex for simple stuff like this 
      //Lowercase comparison for safety 
      if(rel.toLowerCase().indexOf('icon') > -1) { 
       var href = link.getAttribute('href'); 

       //Make sure href is not null/undefined    
       if(href) { 
        //Relative 
        //Lowercase comparison in case some idiot decides to put the 
        //https or http in caps 
        //Also check for absolute url with no protocol 
        if(href.toLowerCase().indexOf('https:') == -1 && href.toLowerCase().indexOf('http:') == -1 
         && href.indexOf('//') != 0) { 

         //This is of course assuming the script is executing in the browser 
         //Node.js is a different story! As I would be using cheerio.js for parsing the html instead of document. 
         //Also you would use the response.headers object for Node.js below. 

         var absoluteHref = window.location.protocol + '//' + window.location.host; 

         if(window.location.port) { 
          absoluteHref += ':' + window.location.port; 
         } 

         //We already have a forward slash 
         //On the front of the href 
         if(href.indexOf('/') == 0) { 
          absoluteHref += href; 
         } 
         //We don't have a forward slash 
         //It is really relative! 
         else { 
          var path = window.location.pathname.split('/'); 
          path.pop(); 
          var finalPath = path.join('/'); 

          absoluteHref += finalPath + '/' + href; 
         } 

         icons.push(absoluteHref); 
        } 
        //Absolute url with no protocol 
        else if(href.indexOf('//') == 0) { 
         var absoluteUrl = window.location.protocol + href; 

         icons.push(absoluteUrl); 
        } 
        //Absolute 
        else { 
         icons.push(href); 
        } 
       } 
      } 
     } 
    } 

    return icons; 
} 
संबंधित मुद्दे