1

I have an external javascript file called test.js as seen below. This file needs user configuration parameters passed to it, in this case user and show values.

<script type="text/javascript">
<!--//
  user = '123';
  show = 'appts';
//-->
</script>
<script src="{{ STATIC_URL }}js/widgets/test.js" type="text/javascript"></script>

Above is currently how I tell 3rd parties to add the script to their own site. However, I cannot help to feel this is a bad way to pass these values i.e. clashes.

Is the way I have done it acceptable? Is there a better way?

3 Answers 3

2

A simple answer would be to append something to your variable names, such as:

<script type="text/javascript">
<!--//
  widget_test_12331_user = '123';
  widget_test_12331_show = 'appts';
//-->
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Um... I'm totally claiming that as an April Fools joke. Yes, that's what happened. Ahem.
2

You can stick to namespace convention "reverted domain":

<script type="text/javascript">
<!--//
if (!org) var org = {};
if (!org.mylibrary_domain) org.mylibrary_domain = {};
if (!org.mylibrary_domain.settings) org.mylibrary_domain.settings = {};
org.mylibrary_domain.settings.user = '123';
org.mylibrary_domain.settings.show = 'appts';
//-->
</script>
<script src="{{ STATIC_URL }}js/widgets/test.js" type="text/javascript"></script>

Comments

2

You can use the concept of javascript namespace (How do I declare a namespace in JavaScript?).

so you can add another js file named testConfig.js, which include :

var yourAppNamespaceTestConfig = {
    user: function(){ return '123' ;} ,
    show: function(){ return 'appts';}
};

then inside test.js, you can read the config by:

var user = yourAppNamespaceTestConfig.user();
var show = yourAppNamespaceTestConfig.show();

And if you're more about OO, try Coffeescript (http://coffeescript.org/). They introduce OO to your javascript.

1 Comment

But why do you need functions for retrieving user and show value?

Your Answer

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.