将参数传递给JS文件的更好方法

时间:2017-11-03 06:51:14

标签: javascript parameter-passing

将参数传递给外部JS文件的更好方法是什么?

方法1:简单的查询字符串参数

SELECT DISTINCT 
dbo.master_order.order_no, 
dbo.master_order.program_no, 
dbo.Setup_size.size_name, 
dbo.Setup_color.color_name, 
dbo.Setup_color.color_no, 
dbo.transaction_production.total_weight, 
dbo.Setup_yarn.yarn_count, 
dbo.Setup_article_order.article_name, 
dbo.master_order.shipment_date, 
@from as reprt,
@to as reprt1, 
dbo.transaction_order.quantity, 
dbo.transaction_order.gsm 

FROM dbo.master_order 
INNER JOIN dbo.transaction_order ON dbo.master_order.order_id = dbo.transaction_order.order_id 
INNER JOIN dbo.transaction_production ON dbo.transaction_order.trans_id = dbo.transaction_production.trans_id 
INNER JOIN dbo.Setup_size ON dbo.transaction_order.size_id = dbo.Setup_size.size_id 
INNER JOIN dbo.Setup_yarn ON dbo.transaction_order.yarn_id = dbo.Setup_yarn.yarn_id 
INNER JOIN dbo.Setup_article_order ON dbo.transaction_order.article_id = dbo.Setup_article_order.article_id 
INNER JOIN dbo.Setup_color ON dbo.transaction_order.color_id = dbo.Setup_color.color_id
 AND dbo.Setup_yarn.color_id = dbo.Setup_color.color_id where dbo.setup_color.color_id in
 (select color_no from dbo.setup_color) where master_order.shipment_date >= @from and master_order.shipment_date<= @to

方法2:通过对象

<script src="https://somedomain.com/somejsfile.js?param1&param2"></script>

1 个答案:

答案 0 :(得分:0)

您可以在脚本文件之前定义全局变量,然后直接将它们访问到脚本文件,如下所示,

/ * =========== index.html =========== * /

<!DOCTYPE html>
<html>
<head>
    <title>Document</title>
</head>
<body>

    <script>
    var global = {
        key: 'value'
    };    
    </script>

    <script src="script.js" ></script>

</body>
</html>

/ * =========== index.html =========== * /

/ * =========== script.js =========== * /

console.log(global.key);

/ * =========== script.js =========== * /