0

Which is the better way to pass parameters to external JS file?

Method 1: Simple querystring parameters

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

Method 2:through object

<script src="https://somedomain.com/somejsfile.js"></script>
<script type="text/javascript">someJSObj.init(["param1&param2"]);</script>
pravid
  • 719
  • 9
  • 27

1 Answers1

0

You can define global vars before the script files and then directly access them to the script file as given below,

/* =========== 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 =========== */

abhijeetwebdev
  • 366
  • 1
  • 4
  • 14
  • How good is this in case of passing multiple and long parameters i.e. parameters with some text with special characters? – pravid Nov 03 '17 at 06:59