Let IE8 support the function.bind of method

  • 2020-03-30 04:09:37
  • OfStack

Mainly solve the "baidu map" official website example bug, extract the following code:


<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
<style type="text/css"> 
body, html {width: 100%;height: 100%;margin:0;font-family:" Microsoft jas black ";} 
#allmap{width:100%;height:500px;} 
p{margin-left:5px; font-size:14px;} 
</style> 
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=39b92e64ae5622663ceceaccd8ab8eb1"></script> 
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> 
<title> Add information window to multiple points </title> 
<script type="text/javascript"> 
if (!Function.prototype.bind) { 
Function.prototype.bind = function (oThis) { 
if (typeof this !== "function") { 
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable"); 
} 
var aArgs = Array.prototype.slice.call(arguments, 1), 
fToBind = this, 
fNOP = function () {}, 
fBound = function () { 
return fToBind.apply(this instanceof fNOP && oThis 
? this 
: oThis, 
aArgs.concat(Array.prototype.slice.call(arguments))); 
}; 
fNOP.prototype = this.prototype; 
fBound.prototype = new fNOP(); 
return fBound; 
}; 
} 
</script> 
</head> 
<body> 
<div id="allmap"></div> 
<p> Click the annotation point to view the simple information window composed of plain text </p> 
</body> 
</html> 
<script type="text/javascript"> 
//Baidu map API function
map = new BMap.Map("allmap"); 
map.centerAndZoom(new BMap.Point(116.417854,39.921988), 15); 
var data_info = [[116.417854,39.921988," Address: wangfujing street, dongcheng district, Beijing 88 On the eighth floor of a Yintai store "], 
[116.406605,39.921585," Address: donghuamen street, dongcheng district, Beijing "], 
[116.412222,39.912345," Address: zhengyi road, dongcheng district, Beijing 5 No. "] 
]; 
var opts = { 
width : 250, //Information window width
height: 80, //Height of information window
title : " Information window " , //Information window title
enableMessage:true//The Settings allow the message window to send short messages
}; 
for(var i=0;i<data_info.length;i++){ 
var marker = new BMap.Marker(new BMap.Point(data_info[i][0],data_info[i][1])); //Create a label
var content = data_info[i][2]; 
map.addOverlay(marker); //Adds annotations to the map
marker.addEventListener("click",openInfo.bind(null,content)); 
} 
function openInfo(content,e){ 
var p = e.target; 
var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat); 
var infoWindow = new BMap.InfoWindow(content,opts); //Create an information window object
map.openInfoWindow(infoWindow,point); //Open information window
} 
</script>

Related articles: