You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Javascript/Jquery获取客户端IP:IE无特殊支持下解决方案求助

Got it, let's break down your problem clearly first: you've found out pure JavaScript can't grab a client's IP, the Chrome-only workarounds don't work on IE, you can't use ActiveX, third-party APIs, and your server doesn't support PHP (so no server-side tweaks beyond your own code). You need a way to get a unique client identifier (not the server IP) in IE.

First, let's get the hard truth out of the way: there's no way to directly get a client's public IP in IE using only front-end code under your constraints. IE's security model and lack of modern APIs block that, and without server-side support (like using a backend language that can fetch the client IP from request headers and send it back) or third-party services, you can't pull the actual IP.

But if your core need is a unique way to identify the client (which is often what people mean when they ask for a "unique IP"), here are workable front-end-only solutions that work in IE:

1. Generate a Persistent Unique ID with Local Storage/IE UserData

You can create a random UUID on the user's first visit, then store it locally so it persists across sessions. This works for all IE versions, including older ones like IE6/7:

function getClientUniqueId() {
    // Try modern localStorage first (IE8+)
    if (window.localStorage) {
        let uniqueId = localStorage.getItem('client_unique_identifier');
        if (!uniqueId) {
            // Generate a simple UUID-like string
            uniqueId = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
                const r = Math.random() * 16 | 0;
                const v = c === 'x' ? r : (r & 0x3 | 0x8);
                return v.toString(16);
            });
            localStorage.setItem('client_unique_identifier', uniqueId);
        }
        return uniqueId;
    }
    // Fallback for IE6/7 using userData behavior
    else if (document.documentElement.addBehavior) {
        const storageElement = document.createElement('div');
        storageElement.style.display = 'none';
        storageElement.addBehavior('#default#userData');
        document.body.appendChild(storageElement);
        storageElement.load('ClientUniqueStorage');
        
        let uniqueId = storageElement.getAttribute('client_unique_identifier');
        if (!uniqueId) {
            uniqueId = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
                const r = Math.random() * 16 | 0;
                const v = c === 'x' ? r : (r & 0x3 | 0x8);
                return v.toString(16);
            });
            storageElement.setAttribute('client_unique_identifier', uniqueId);
            storageElement.save('ClientUniqueStorage');
        }
        return uniqueId;
    }
    // Last resort: hash browser/device info for a session-unique ID
    else {
        const deviceInfo = navigator.userAgent + navigator.platform + screen.width + screen.height + screen.colorDepth;
        // Simple hash function
        return Array.from(deviceInfo).reduce((hash, char) => {
            hash = ((hash << 5) - hash) + char.charCodeAt(0);
            return hash & hash;
        }, 0).toString(16);
    }
}

This gives you a stable, unique identifier that stays with the client across visits (unless they clear their browser storage). It's fully front-end, no server changes needed, and works in all IE versions.

2. Session-Only Unique ID from Browser/Device Attributes

If you don't need the identifier to persist across sessions, you can combine various browser and device properties (like user agent, screen size, language) and hash them into a unique string. This isn't as persistent as the storage method, but it works for single-session identification without any storage:

function getSessionUniqueId() {
    const attributes = [
        navigator.userAgent,
        navigator.platform,
        navigator.language,
        screen.width,
        screen.height,
        screen.colorDepth,
        new Date().getTimezoneOffset()
    ];
    const attributeString = attributes.join('|');
    return Array.from(attributeString).reduce((hash, char) => {
        hash = ((hash << 5) - hash) + char.charCodeAt(0);
        return hash & hash;
    }, 0).toString(16);
}

Important Note

If you absolutely must have the actual public IP address (not just a unique identifier), there's no solution under your current constraints. IE doesn't expose any front-end API to fetch the public IP, and without server-side code (like using ASP, Node.js, or another supported backend language to read the X-Forwarded-For or Remote-Addr headers and send it to the client) or third-party APIs, you can't retrieve it. In that case, you'd need to either relax your server-side constraints or adjust your requirement to use a client identifier instead.

内容的提问来源于stack exchange,提问作者Janardhan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:23:56