Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> HTML

HTML5 लोकल स्टोरेज में कुंजी मौजूद है या नहीं जांचें


लोकल स्टोरेज में कुंजी जांचना

HTML5 localStorage API में कुंजी मौजूद है या नहीं, यह जांचने के कई तरीके हैं।

विधि 1: getItem() और null जांच (अनुशंसित)

// कुंजी मौजूद है और मान null नहीं है
if (localStorage.getItem('user') !== null) {
    console.log('कुंजी मौजूद है:', localStorage.getItem('user'));
} else {
    console.log('कुंजी मौजूद नहीं है या मान null है');
}

// छोटा रूप
const user = localStorage.getItem('user');
if (user) {
    // मान truthy है (null, undefined, खाली स्ट्रिंग नहीं)
}

विधि 2: in ऑपरेटर

if ('user' in localStorage) {
    console.log('कुंजी मौजूद है (मान null भी हो सकता है)');
} else {
    console.log('कुंजी मौजूद नहीं है');
}

विधि 3: hasOwnProperty (Object.prototype पर)

if (localStorage.hasOwnProperty('user')) {
    console.log('कुंजी मौजूद है');
}

विधि 4: try-catch के साथ (निजी/इनकॉग्निटो मोड के लिए)

function hasLocalStorageKey(key) {
    try {
        return key in localStorage;
    } catch (e) {
        // निजी मोड / स्टोरेज अक्षम
        console.warn('लोकल स्टोरेज अनुपलब्ध:', e);
        return false;
    }
}

तुलना तालिका

थ>विधि थ>null मान लौटाता है? थ>अपवाद फेंक सकता है? थ>उपयोग मामला
getItem(key) !== null नहीं (false लौटाता है) नहीं सबसे आम; मान भी प्राप्त करता है
key in localStorage हाँ (true लौटाता है) नहीं (आमतौर पर) केवल उपस्थिति जांच, मान नहीं चाहिए
hasOwnProperty(key) हाँ हाँ (निजी मोड में) कम आम

पूर्ण उदाहरण: उपयोगकर्ता सत्र प्रबंधन

<!DOCTYPE html>
<html>
<body>
    <h2>लोकल स्टोरेज कुंजी जांच डेमो</h2>
    
    <div style="margin-bottom: 15px;">
        <input type="text" id="keyInput" placeholder="कुंजी नाम" value="user">
        <input type="text" id="valueInput" placeholder="मान" value="जॉन">
        <button onclick="setItem()">सेट करें</button>
        <button onclick="checkKey()">जांचें</button>
        <button onclick="removeItem()">हटाएँ</button>
        <button onclick="clearAll()">सभी साफ़ करें</button>
    </div>
    
    <div style="margin-bottom: 15px;">
        <strong>परिणाम:</strong>
        <pre id="result" style="background:#f5f5f5; padding:10px; border-radius:4px;"></pre>
    </div>
    
    <h3>वर्तमान स्टोरेज सामग्री:</h3>
    <div id="storageView"></div>
    
    <script>
        const result = document.getElementById('result');
        const storageView = document.getElementById('storageView');
        
        function updateStorageView() {
            storageView.innerHTML = '';
            for (let i = 0; i < localStorage.length; i++) {
                const key = localStorage.key(i);
                const value = localStorage.getItem(key);
                const div = document.createElement('div');
                div.style.cssText = 'padding:5px; border-bottom:1px solid #eee; font-family:monospace;';
                div.textContent = `${key}: "${value}"`;
                storageView.appendChild(div);
            }
            if (localStorage.length === 0) {
                storageView.textContent = '(खाली)';
            }
        }
        
        function setItem() {
            const key = document.getElementById('keyInput').value.trim();
            const value = document.getElementById('valueInput').value;
            if (key) {
                localStorage.setItem(key, value);
                log(`सेट: ${key} = "${value}"`);
                updateStorageView();
            }
        }
        
        function checkKey() {
            const key = document.getElementById('keyInput').value.trim();
            if (!key) return log('कुंजी दर्ज करें');
            
            const exists = key in localStorage;
            const value = localStorage.getItem(key);
            const getItemNull = localStorage.getItem(key) !== null;
            
            log(`कुंजी: "${key}"`);
            log(`  'in' ऑपरेटर: ${exists}`);
            log(`  getItem() !== null: ${getItemNull}`);
            log(`  मान: "${value}"`);
        }
        
        function removeItem() {
            const key = document.getElementById('keyInput').value.trim();
            if (key) {
                localStorage.removeItem(key);
                log(`हटाया: ${key}`);
                updateStorageView();
            }
        }
        
        function clearAll() {
            localStorage.clear();
            log('सभी साफ़ किया गया');
            updateStorageView();
        }
        
        function log(msg) {
            result.textContent += msg + '\n';
            result.scrollTop = result.scrollHeight;
        }
        
        // प्रारंभिक दृश्य
        updateStorageView();
    </script>
</body>
</html>

सत्र स्टोरेज (sessionStorage)

उसी API का उपयोग sessionStorage के लिए करें (टैब/विंडो बंद होने पर साफ़ हो जाता है):

if (sessionStorage.getItem('tempData') !== null) {
    // सत्र-विशिष्ट डेटा
}

TypeScript में टाइप-सेफ जांच

function getLocalStorageItem(key: string, defaultValue: T): T {
    try {
        const item = localStorage.getItem(key);
        return item !== null ? JSON.parse(item) : defaultValue;
    } catch {
        return defaultValue;
    }
}

function setLocalStorageItem(key: string, value: T): void {
    try {
        localStorage.setItem(key, JSON.stringify(value));
    } catch (e) {
        console.error('लोकल स्टोरेज सेट त्रुटि:', e);
    }
}
  1. HTML DOM लोकल स्टोरेज clear() मेथड HTML DOM लोकल स्टोरेज clear() मेथड

    HTML DOM लोकल स्टोरेज clear() मेथड का उपयोग पूरे लोकल स्टोरेज एंट्रीज़ को साफ़ करने के लिए किया जाता है। सिंटैक्स निम्नलिखित सिंटैक्स है − localStorage.clear() या sessionStorage.clear() उदाहरण आइए LocalStorage() मेथड गुण का एक उदाहरण देखें − <!DOCTYPE html> <html> <head> <title

  1. HTML DOM Storage setItem() मेथड HTML DOM Storage setItem() मेथड

    HTML DOM Storage setItem() मेथड का उपयोग एक स्टोरेज ऑब्जेक्ट आइटम को दिए गए की नाम और मान पास करके बनाने या अपडेट करने के लिए किया जाता है। सिंटैक्स Storage setItem() मेथड का सिंटैक्स: localStorage.setItem(keyname, value); या sessionStorage.setItem(keyname, value); यहां, keyname स्ट्रिंग प्रकार का

  1. HTML DOM Storage key() मेथड HTML DOM Storage key() मेथड

    HTML DOM Storage key() मेथड का उपयोग स्टोरेज ऑब्जेक्ट में दिए गए इंडेक्स पर की नाम को वापस करने के लिए किया जाता है। इंडेक्स को key() मेथड के पैरामीटर के रूप में पास किया जाता है। स्टोरेज ऑब्जेक्ट सेशन ऑब्जेक्ट या localStorage ऑब्जेक्ट हो सकता है। सिंटैक्स LocalStorage का उपयोग करके Storage key() म