2010-10-13 13 views

risposta

57
switch(window.location.protocol) { 
    case 'http:': 
    case 'https:': 
    //remote file over http or https 
    break; 
    case 'file:': 
    //local file 
    break; 
    default: 
    //some other protocol 
} 
+2

Aggiungi un 'https:' caso pure e sarai a posto. – Blackcoat

+1

Questo non è vero. Un sito può essere ospitato localmente e utilizzare ancora il protocollo 'http' quando è ospitato su un server Web localmente. – Nes

1

Altri modi per farlo:

if (/^h/.test(document.location)) { 
    // remote file over http or https 
} else { 
    // local file 
} 

o

if (document.location.host) { 
    // remote file over http or https 
} else { 
    // local file 
} 

o (slow, non raccomandato)

if ((''+document.location).indexOf('http') === 0) { 
// if (document.location.protocol.indexOf('http') === 0) { // another way 
    // remote file over http or https 
} else { 
    // local file 
} 
Problemi correlati