5

I have a question about the global script, I'm using the following script right now since those functions are used globally on every page. (before I have the same functions like unit converter function defined in 8 places, now I move it to the global script so I only define it once.)

My question is, is this a valid way or best practice to do? I really need them to defined globally and loaded before other scripts are calling those methods.

I have searched and article like this suggest not use global functions. https://www.w3schools.com/js/js_best_practices.asp

Avoid Global Variables

Minimize the use of global variables.

This includes all data types, objects, and functions.

Global variables and functions can be overwritten by other scripts.

Use local variables instead, and learn how to use closures.

<!DOCTYPE html>
<html>
<head>
 <script type="text/javascript">
 /*-----------------------------------------------------
 global function for Set/Get Cookie
 ------------------------------------------------------*/
 function setCookie(cname,cvalue,exdays) {
 var d = new Date();
 d.setTime(d.getTime() + (exdays*24*60*60*1000));
 var expires = "expires=" + d.toGMTString();
 document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
 }
 
 function getCookie(name) {
 var value = "; " + document.cookie;
 var parts = value.split("; " + name + "=");
 if (parts.length == 2) return parts.pop().split(";").shift();
 }
 /*-----------------------------------------------------
 global function for ...
 ------------------------------------------------------*/
 function getLBS_Global(p, q, u, b, wb) {
 if (b === null || wb === null) {
 alert("Error in unit conversion, please retry");
 }
 var out = {};
 switch (u) {
 case "8": // A bushel
 out.q = (q * b);
 out.p = (p / b);
 break;
 case "10": // lbs
 out.q = q;
 out.p = p;
 break;
 case "11": // cwt
 out.q = (q * 100);
 out.p = (p / 100);
 break;
 case "12": // metric tonne
 out.q = (q * 2204.62262);
 out.p = (p / 2204.62262);
 break;
 case "136": // W bushel
 out.q = (q * wb);
 out.p = (p / wb);
 break;
 }
 return out;
 }
 </script>
</head>
<body>
...
</body>
asked May 16, 2017 at 19:12
3
  • 3
    Eh, yeah. It's perfectly valid and best practise. 😊 Commented May 16, 2017 at 19:13
  • best would be to wrap it into an Object, so that youre not intercepting with others function names, but thats not neccessary if all code is written by you... Commented May 16, 2017 at 19:16
  • 1
    @PraveenKumar thanks, because I have heard from different people with different choice, now I feels better ;D Commented May 16, 2017 at 19:22

2 Answers 2

4

yes it's valid.

if you don't want to pollute the global scope (and to avoid clash by name) a best practise is to wrap your functions inside an object and expose the object to the global scope

this is an example, but the idea is to bundle together the functions base on their behaviour

<script type="text/javascript">
 (function(global){
 function setCookie(cname,cvalue,exdays) {
 var d = new Date();
 d.setTime(d.getTime() + (exdays*24*60*60*1000));
 var expires = "expires=" + d.toGMTString();
 document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
 }
 function getCookie(name) {
 var value = "; " + document.cookie;
 var parts = value.split("; " + name + "=");
 if (parts.length == 2) return parts.pop().split(";").shift();
 }
 global.bundleObj = {
 setCookie: setCookie,
 getCookie: getCookie
 }
 })(window)
</script>

then anywhere in your code

<script>
 window.bundleObj.setItem()
</script>
answered May 16, 2017 at 19:18

3 Comments

thanks, when I calling those function I use global.stuff.setCookie()?
i've edited my answer. just use window :) i used global inside the function to be more generic.
This looks better, bundle functions really make sense, and the way you define global functions looks better. Thanks for the help!
2

Yeah, that seems fine. If you're using it all over the page, you might as well have it defined in the global scope. The only reason I would recommend otherwise is if you have a lot of functions or you have code intermingling with code other people wrote. If it's pretty short and no one else is contributing, this way is just easier.

answered May 16, 2017 at 19:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.