一、JS獲取當前IP地址
在互聯網上,IP地址是標識計算機或者其他設備的一組編號,也是網際網路協議(IP協議)家族的統稱。
使用JavaScript可以輕鬆地獲取當前用戶的IP地址。下面是獲取當前IP地址的代碼:
function getIPAddress() { fetch('https://api.ipify.org?format=json') .then(response => response.json()) .then(data => console.log(data.ip)) .catch(error => console.error(error)); } getIPAddress();
代碼解釋:
上面的代碼中使用了ES6的fetch API來發送HTTP請求來獲取JSON格式的響應。然後解析響應並講IP地址列印在控制台上。
二、JS獲取當前時間
獲取當前時間可以幫助我們處理時間相關的問題,比如在網頁中顯示時間等操作。
使用JavaScript獲取當前時間的方法很簡單。下面是獲取當前時間的代碼:
const now = new Date(); console.log(now);
代碼解釋:
上面的代碼中使用了Date()函數來獲取當前時間,然後將其存儲在now變數中。最後,我們將其列印在控制台上。
三、JS獲取當前年份
獲取當前年份也是常見的操作。在網頁中,我們經常需要當前年份來顯示版權信息或者其他相關信息。
使用JavaScript獲取當前年份的方法也很簡單。下面是獲取當前年份的代碼:
const now = new Date(); const year = now.getFullYear(); console.log(year);
代碼解釋:
上面的代碼中,我們使用了Date()函數獲取當前時間,然後通過調用getFullYear()方法來獲取當前年份。最後將其列印在控制台上。
四、JS獲取當前時間戳
時間戳是計算機存儲時間的一種方式。它表示從某個固定時間點到當前時間之間的時間間隔(秒數或毫秒數)。
JavaScript中,可以使用Date對象的 getTime() 方法獲取當前時間戳。具體方法如下:
const now = new Date(); const timestamp = now.getTime(); console.log(timestamp);
代碼解釋:
上面的代碼中,我們使用了Date()函數獲取當前時間,然後通過調用 getTime() 方法獲取當前時間戳。最後將其列印在控制台上。
五、JS獲取當前位置信息
在移動端的Web應用中,獲取當前位置信息非常重要。它可以幫助我們實現很多有趣的功能,比如顯示當前附近的商家、查找當地的熱門景點等。
使用JavaScript可以輕鬆地獲取當前位置信息。具體方法如下:
function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { console.log("Geolocation is not supported by this browser."); } } function showPosition(position) { console.log("Latitude: " + position.coords.latitude + "\nLongitude: " + position.coords.longitude); } getLocation();
代碼解釋:
上面的代碼中,我們使用了navigator.geolocation對象來獲取位置信息。如果瀏覽器支持此功能,則調用getCurrentPosition()方法來獲取當前位置的經度和緯度。最後將其列印在控制台上。
六、JS獲取當前年度
獲取當前年度(即當前所在周的年份)也是常見的操作。比如在日曆應用中,我們需要獲取當前周的年份。下面是獲取當前年度的代碼:
const now = new Date(); const year = now.getFullYear(); const week = Math.ceil((now - new Date(year, 0, 1)) / 86400000 / 7); console.log(year + " 年第 " + week + " 周");
代碼解釋:
上面的代碼中,我們首先使用Date對象獲取當前時間的年份。然後使用Math.ceil()函數將時間差(以周為單位)向上舍入。最後將結果列印在控制台上。
七、JS獲取當前URL路徑
獲取當前URL路徑可以幫助我們在網頁中實現跳轉等相關操作。下面是獲取當前URL路徑的代碼:
const currentUrl = window.location.href; console.log(currentUrl);
代碼解釋:
上面的代碼中,我們使用window.location.href屬性獲取當前URL路徑,並將其列印在控制台上。
八、JS獲取用戶的IP地址
除了獲取當前用戶的IP地址,我們還可以獲取訪問我們網站的用戶的IP地址。下面是獲取用戶IP地址的代碼:
function getUserIP() { fetch('https://api.ipify.org?format=json') .then(response => response.json()) .then(data => console.log(data.ip)) .catch(error => console.error(error)); } getUserIP();
代碼解釋:
上面的代碼中,我們使用fetch()函數獲取一個API介面返回的JSON對象。然後解析出其中的IP地址,並將其列印在控制台上。
九、JS獲取本地IP
獲取本地IP可以幫助我們在網路通信中實現各種有用的功能。下面是獲取本地IP的代碼:
function getLocalIP() { window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; if (typeof window.RTCPeerConnection == "undefined") { console.error("Your browser does not support RTCPeerConnection."); return; } let pc = new RTCPeerConnection(); pc.createDataChannel(""); pc.createOffer() .then(function(offer) { return pc.setLocalDescription(offer); }) .then(function() { let lines = pc.localDescription.sdp.split("\n"); lines.forEach(function(line) { if (line.indexOf("a=candidate") == 0) { let match = line.match(/([0-9\.]+)[^\d]*$/); if (match && match.length > 1) { console.log(match[1]); } } }); }) .catch(function(reason) { console.error(reason); }); } getLocalIP();
代碼解釋:
上面的代碼中,我們使用RTCPeerConnection對象來獲取本地IP地址。我們創建一個新的RTCPeerConnection對象和一個別名通道,並創建一個新的SDP offer來激活連接。最後,通過解析SDP字元串來獲取本地IP地址。
原創文章,作者:HICW,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/137905.html