简体   繁体   English

基于本地/产品的Javascript URL

[英]Javascript URL base on local/prod

I want to refer to the domain base in Javascript, which would be http://localhost/project in my local environment and http://www.project.com (or https depending on the page). 我想引用Javascript中的域库,该域库在我的本地环境中为http://localhost/projecthttp://www.project.com (或https,具体取决于页面)。 I'm currently defining 2 global variables, one for links to other non-https pages (where I don't want to use the fact that the current page may be https) and one that picks up whether the page is https or not, to load resources without having an unsecure elements warning. 我目前正在定义2个全局变量,一个用于链接到其他非https页面(我不想使用当前页面可能是https的事实),而一个用于选择页面是否为https的变量,加载资源而不会发出不安全元素警告。

Local version: 本地版本:

var link_base_url = 'http://localhost/project/'; //for links to other non-secure pages
if(window.location.protocol == 'https:') var base_url = 'http://localhost/project/'; //SSL version for displaying resources or calling ajax scripts
else var base_url = 'http://localhost/project/'; //non-SSL version for displaying resources or calling ajax scripts

Prod version: 产品版本:

var link_base_url = 'http://www.project.com/'; //for links to other non-secure pages
if(window.location.protocol == 'https:') var base_url = 'https://www.project.com/'; //SSL version for displaying resources or calling ajax scripts
else var base_url = 'http://www.project.com/'; //non-SSL version for displaying resources or calling ajax scripts

I am no expert on Javascript but I think this is probably not the cleanest way to do it: it forces me to change these few lines of code each time I want to deploy this script, and from what I understand using global variables is not that good. 我不是Java语言方面的专家,但是我认为这可能不是最干净的方法:每次我要部署此脚本时,它都会迫使我更改这几行代码,据我了解,使用全局变量并不是好。

Is there a better way to do this? 有一个更好的方法吗? I am not using any JS framework like Ember. 我没有使用像Ember这样的JS框架。

Thanks for the input! 感谢您的输入!

Do you need something like this? 你需要这样的东西吗?

var link_base_url = 'http://'+window.location.host+'/';
var base_url = /\.com$/.test(window.location.hostname) ? window.location.protocol+'//'+window.location.host+'/' : link_base_url;

You could use location.href, which contains the entire URL of the current page, to check your current environment ( http://www.w3schools.com/jsref/prop_loc_href.asp ). 您可以使用location.href(包含当前页面的整个URL)来检查当前环境( http://www.w3schools.com/jsref/prop_loc_href.asp )。

var checkURL = location.href, base_url = '', link_base_url = '';

if (/\/\/localhost\/project\//.test(checkURL)) {
    link_base_url = 'http://localhost/project/';
    if (window.location.protocol == 'https:') {
        base_url = 'https://localhost/project/';
    } else {
        base_url = 'http://localhost/project/';
    }
} else {
    link_base_url = 'http://www.project.com/';
    if (window.location.protocol == 'https:') {
        base_url = 'https://www.project.com/';
    } else {
        base_url = 'http://www.project.com/';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM