首页 > 代码库 > shortcut icon使用

shortcut icon使用

第一部分:

之前这么久竟然一直不知道shortcut icon的使用,下面这个简单的实例即可说明:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>link标签实现shortcut</title>    <link rel="shortcut icon" type="images/x-icon" href=http://www.mamicode.com/"./my.ico"> </head><body>    Hello, please look at the shortcur icon.  </body></html>

其中rel必须是shortcut icon, type必须是images/x-icon,href即为一张ico的图片即可。

http://www.ico.la/

此网站可以在线制作ico图片。 

 

 

第二部分: node中使用favicon

如下所示,创建app.js,在当前目录下创建public,存放ico图片,如下:

var http = require(http);var path = require(path); // path模块大多是为了使用path.join() 方法的。var fs = require(fs);var url = require(url); //  url模块大多是为了处理req的,即利用url.parse可以将url进行解析。// var server = http.createServer();// Location of your favicon in the filesystem.var FAVICON = path.join(__dirname, public, favicon.ico);var server = http.createServer(function(req, res) {  var pathname = url.parse(req.url).pathname;  // If this request is asking for our favicon, respond with it.  if (req.method === GET && pathname === /favicon.ico) {    // MIME type of your favicon.    //    // .ico = ‘image/x-icon‘ or ‘image/vnd.microsoft.icon‘    // .png = ‘image/png‘    // .jpg = ‘image/jpeg‘    // .jpeg = ‘image/jpeg‘
  // 注意icon的头部设置为image/x-icon
res.setHeader(Content-Type, image/x-icon); // Serve your favicon and finish response. // // You don‘t need to call `.end()` yourself because // `pipe` will do it automatically.
  // 即利用pipe() 方法将文件pipe给res。
fs.createReadStream(FAVICON).pipe(res); return; } // This request was not asking for our favicon, // so you can handle it like any other request. res.end();});// Listen on port 3000.//// This line is not relevant to this answer, but// it would feel incomplete otherwise.server.listen(3000);

 

shortcut icon使用