简体   繁体   English

将变量注入跨域脚本

[英]Injecting variables into cross domain scripts

I've written several javascript third-parties and browser extensions when I have the issue of wanting to change variable values between my local environment and my production environment. 当我想在本地环境和生产环境之间更改变量值时,我已经编写了多个javascript第三方和浏览器扩展。

In other languages I can simply create a file called settings or config or globals and import or include or require that file in the script I want to use the variables in, but frontend javascript doesn't seem to have namespacing or script inclusion. 在其他语言中,我可以简单地创建一个名为settings或config或globals的文件,然后在要使用变量的脚本中导入或包含该文件或要求该文件,但是前端javascript似乎没有命名空间或包含脚本。 So I am wondering what can be done to easily change variable values on deploy. 因此,我想知道如何才能在部署时轻松更改变量值。

An example of the issue I'm talking about: 我正在谈论的问题的一个示例:

say I have a script that is meant to be loaded by third parties: 说我有一个打算由第三方加载的脚本:

(function(){
    var path = 'http://localhost:5000/some_script.js?callback=?&value=1';

    $.getJSON(path, function(data){
        console.log(data);
    });
})();

Because it is a third party script (it is meant to be loaded on domains other than the one it is hosted on) I can't use a partial but obviously I don't want path to be set to localhost:5000 in production. 因为它是第三方脚本(它打算加载到托管脚本之外的域上),所以我不能使用部分脚本,但显然我不希望在生产环境中将路径设置为localhost:5000

What should I be doing? 我该怎么办?

You should have a build step in your development process to convert these development values to production values using a build script. 您应该在开发过程中具有构建步骤,以使用构建脚本将这些开发值转换为生产值。

Something like Apache Ant or something. Apache Ant之类的东西。

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

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