script-querystring.js
Synopsis
This script enables the use of querystring parameters for external JavaScripts.
Advantages of this may be:
- No need to pollute your global JavaScript environment with variables that are needed for just 1 script.
- No more left-behind variables in the
<head/>
section of your webpages, that nobody dares remove, because nobody remembers what they were for. - Easily locate the external variables your scripts use.
- Etcetera…
License
Copyright © 2016 Rob la Lau <https://ohreally.nl/>
This work is licensed under a Creative Commons Attribution 4.0 International License (https://creativecommons.org/licenses/by/4.0/deed.en).
Please be aware that minimizing this script does not mean that you don't have to include the copyright and author info.
Download
After reading the license information, you can download the current version of script-querystring.js
by clicking → here ←going to https://js.ohreally.nl/download/script-querystring.js
Please, host the script with your own website instead of linking to the scripts at js.ohreally.nl directly.
Read thisRead https://js.ohreally.nl/hiy if you want to know why.
Dependencies
This script depends on script-url.js (https://js.ohreally.nl/info-url).
See Browser compatibility below for the list of supported/supporting browsers.
Installation
Download and install script-url.js.
Save script-querystring.js
to the root of your website ( http://www.example.com/script-querystring.js
).
Put this in the <head/>
section of your webpages:
<script type="text/javascript" src="/script-url.js"></script> <script type="text/javascript" src="/script-querystring.js"></script>
Usage
This scripts defines 1 function:
jsQueryParam(script, param)
- Required parameters:
- path to the script you want to get the parameter for
- name of the parameter you want to get
Return value:
- the value for the given querystring parameter
see script-url.js → getQueryParam() (https://js.ohreally.nl/info-url#meth_getqueryparam) for info on the possible formats
Examples
Save this as /qs-test.js
:
var welcomeStrings = { 'en' : 'Welcome visitor', 'nl' : 'Welkom bezoeker', 'fr' : 'Bienvenue visiteur', 'de' : 'Willkommen Besucher' }; function beFriendly() { language = jsQueryParam('/qs-test.js', 'language'); if (! Boolean(welcomeStrings[language])) { document.getElementById('greeting').innerHTML = ':-)'; } else { document.getElementById('greeting').innerHTML = welcomeStrings[language]; } } window.addEventListener('load', beFriendly);
Save this as /qs-test.html
:
<html> <head> <title>Test</title> <script type="text/javascript" src="/script-url.js"></script> <script type="text/javascript" src="/script-querystring.js"></script> <script type="text/javascript" src="/qs-test.js?language=nl"></script> </head> <body> <div id="greeting"></div> </body> </html>
Open /qs-test.html
in your browser.
Change the language=
parameter in qs-test.html
, and reload.
Browser compatiblity
Firefox | 4.0 |
Chrome | 6 |
Internet Explorer | 9 |
Opera | 12 |
Safari | 5.1 |