JS/javascript (6) 썸네일형 리스트형 [javascript] js 소수점 1 자릿수가 0인 경우 소수점 이하 버리고 소수점 1 자릿수가 0보다 큰 경우 소수점 이하 올림 소수점 1 자릿수가 0인 경우 소수점 이하 버리고 소수점 1 자릿수가 0보다 큰 경우 소수점 이하 올림 function processNumber(num) { var rounded = Math.round(num * 10) / 10; // 소수점 1 자릿수에서 반올림 if (rounded % 1 === 0) { // 소수점 1 자릿수가 0인 경우 return Math.floor(rounded); // 소수점 이하 버림 } else { // 소수점 1 자릿수가 0보다 큰 경우 return Math.ceil(rounded); // 소수점 이하 올림 } } console.log(processNumber(10.000000000001137)); 결과 : 10 [js] http로 접속하면 https로 redirect http로 접속을 하면 https로 리다이렉트 하는 방법은 여러가지가 있다. 그중 js로 처리 하는 방법이다. if (document.location.protocol == 'http:') { document.location.href = document.location.href.replace('http:', 'https:'); } location.href 사용법 절대경로 location.href = "https://naver.com/index.php" 상대경로 location.href = "index.php" [javascript] 천단위 찍기 var price = 10000; price.toLocaleString(); // 10,000 [javascript] media query if (window.matchMedia('(max-width: 768px)').matches) { // do functionality on screens smaller than 768px } $(window).resize(function() { if ($(this).width() < 1024) { // your code } else { // your code } }); [javascript] URL 형식인지를 체크하는 정규식 // url 형식인지를 체크( http, https 를 포함하는 형식 ) function checkUrlForm(strUrl) { var expUrl = /^http[s]?\:\/\//i; return expUrl.test(strUrl); } 이전 1 다음