javascript – How to draw polygons on an HTML5 canvas?-ThrowExceptions

Exception or error: I need to know how to draw polygons on a canvas. Without using jQuery or anything like that. How to solve: Create a path with moveTo and lineTo (live demo): var ctx = canvas.getContext(‘2d’); ctx.fillStyle = ‘#f00’; ctx.beginPath(); ctx.moveTo(0, 0); ctx.lineTo(100,50); ctx.lineTo(50, 100); ctx.lineTo(0, 90); ctx.closePath(); ctx.fill(); ### //poly [x,y, x,y, x,y…..]; …

html – Check if a div does NOT exist with javascript-ThrowExceptions

Exception or error: Checking if a div exists is fairly simple if(document.getif(document.getElementById(‘if’)){ } But how can I check if a div with the given id does not exist? How to solve: var myElem = document.getElementById(‘myElementId’); if (myElem === null) alert(‘does not exist!’); ### if (!document.getElementById(“given-id”)) { //It does not exist } The statement document.getElementById(“given-id”) returns …

javascript – Open popup and refresh parent page on close popup-ThrowExceptions

Exception or error: I opened a popup window by window.open in JavaScript, i want to refresh parent page when I close this popup window.(onclose event?) how can I do that? window.open(“foo.html”,”windowName”, “width=200,height=200,scrollbars=no”); How to solve: You can access parent window using ‘window.opener‘, so, write something like the following in the child window: <script> window.onunload = …

javascript – How to get visitor's location (i.e. country) using geolocation?-ThrowExceptions

Exception or error: This question already has answers here: Get visitors language & country code with javascript (client-side) [duplicate] (3 answers) Closed 2 years ago. I’m trying to extend the native geolocation function if(navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { var latitude = position.coords.latitude; var longitude = position.coords.longitude; }); } so that I can use the visitor’s country …

Convert an ISO date to the date format yyyy-mm-dd in JavaScript-ThrowExceptions

Exception or error: How can I get a date having the format yyyy-mm-dd from an ISO 8601 date? My  8601 date is 2013-03-10T02:00:00Z How can I get the following? 2013-03-10 How to solve: Try this date = new Date(‘2013-03-10T02:00:00Z’); date.getFullYear()+’-‘ + (date.getMonth()+1) + ‘-‘+date.getDate();//prints expected format. Update:- As pointed out in comments, I am updating the …