I'm a newbie to javascript. How can I detect if my javascript is being run from a web site (http://) vs a local file.
我是javascript的新手。如何检测我的javascript是从网站(http://)运行到本地文件。
2 个解决方案
#1
58
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
1
Other ways to do this:
其他方法:
if (/^h/.test(document.location)) {
// remote file over http or https
} else {
// local file
}
or
if (document.location.host) {
// remote file over http or https
} else {
// local file
}
or (slow, not recommended)
或(慢,不推荐)
if ((''+document.location).indexOf('http') === 0) {
// if (document.location.protocol.indexOf('http') === 0) { // another way
// remote file over http or https
} else {
// local file
}
#1
58
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
1
Other ways to do this:
其他方法:
if (/^h/.test(document.location)) {
// remote file over http or https
} else {
// local file
}
or
if (document.location.host) {
// remote file over http or https
} else {
// local file
}
or (slow, not recommended)
或(慢,不推荐)
if ((''+document.location).indexOf('http') === 0) {
// if (document.location.protocol.indexOf('http') === 0) { // another way
// remote file over http or https
} else {
// local file
}