2010-10-13 22 views

cevap

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

bir 'https' Ekle ve gitmek için iyi olacaksın. – Blackcoat

+1

Bu doğru değil. Bir site yerel olarak barındırılabilir ve yerel olarak bir web sunucusunda barındırıldığında 'http' protokolünü kullanmaya devam edebilir. – Nes

1

diğer yollar: yanı vaka:

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

veya

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

veya (önerilmez slow,)

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