jQuery Script Parameters: Retrieve the parameters of script tag parameters

Recommend this page to a friend!
  Info   View files Example   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 65 All time: 480 This week: 6Up
Version License JavaScript version Categories
thrak-script 1.0.1BSD License1.0jQuery, Language
Description Author

This is a jQuery plugin that can retrieve the parameters of script tag parameters.

It can take a given script tag on the page and parses its URL to parse and extract its parameters.

Currently it can extract URL domain, base path, file path, the tag DOM object, and any URL query parameters.

Innovation Award
JavaScript Programming Innovation award nominee
November 2015
Number 2
A remote JavaScript script may be included using its URL and additional parameters that can configure its behavior.

However, if the script is a static file, the script code will always work the same way regardless of any parameters passed in the script retrieval request.

The jQuery plugin can extract parameters from a script tag on the page that was used to include a given script. This way the script can adjust its behavior according to the parameters passed to the script request.

Manuel Lemos
Picture of Christian Vigh
  Performance   Level  
Name: Christian Vigh <contact>
Classes: 7 packages by
Country: France France
Age: 56
All time rank: 443 in France France
Week rank: 6 Up2 in France France Up
Innovation award
Innovation award
Nominee: 4x

Winner: 2x

Details

INTRODUCTION

Ever wanted to pass url parameters to your scripts using the &lt;script&gt; tag then retrieve them from your scripts once they are loaded ?

script.js (thrak.script-1.0.1.js, to be more precise) is made for you. It is designed as kinda jQuery extension function, $.script().

HOW TO USE IT ?

There are two contexts you need to consider for using script.js :

  1. The context of the caller ; this typically is you html page, which contains the &lt;script&gt; tags that will invoke your script(s).
  2. The context of the callee, ie your script : this is where you need to retrieve the url parameters that were passed by the caller

CONTEXT OF THE CALLER (YOUR HTML PAGE)

In your html page, you first need :

  1. A reference to jQuery, for example :

    <script language='javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js'&gt;&lt;/script>

  2. A reference to script.js :

    <script language="javascript" type="text/javascript" src="thrak.script-1.0.1.js"></script>

  3. A reference to your own script(s), for example :

    <script language="javascript" type="text/javascript" src="sample_include_1.js?script1_param1=value1&script1_param2=value2&script1_paramn=valuen"></script>

CONTEXT OF THE CALLEE (YOUR SCRIPT)

When your script executes, the $.script() function is available to it. It returns an object that contains data specific to your script, such as url parameters. You are free to use it (or not) but keep in mind the following aspects :

  1. $.script() returns the data related to the last &lt;script&gt; tag found in the calling document (your html page). This is in fact the &lt;script&gt; tag that invoked your script. Although it could be possible, the $.script() function is not designed to retrieve data from other scripts that may have been included before yours.
  2. The return value of $.script() is local to the scope of your script. Once you get out of your script's scope, $.script() may reference another &lt;script&gt; tag. This is why it is really important to save the function's return value if you want to use it later.

The following paragraphs give some various "techniques" to save this data.

SAVING DATA THE CLASSICAL WAY

This is the simplest way : just declare a global variable :

	// File : save_classical.js
	var 	save_classical_script_data 	=  $. script ( ) ;

The key issue with this approach is that you have to take care of global variable names.

SAVING DATA THE JQUERY WAY

jQuery plugins typically contain the following lambda-function construction, which is an easy way to map the jQuery global object to the well-known $ ; you simply define a closure and call it immediately, supplying the jQuery global object as a parameter :

	( function ( $ )

{

			// The "$" variable is now available to your plugin
	    } ( jQuery ) ) ;

Just do the same way within your script :

	// File : save_jquery.js
	( function ( $, $script )

{

			// Now, "$" is a synonym of the jQuery object, and $script is available
			// throughout your plugin to reference script invocation data
	    } ( jQuery, $. script ( ) ) ) ;

REFERENCE

This section gives a detailed description of the object returned by the $.script() function, which has no parameters.

The example data values all refer to this example script tag :

	<script language="javascript" src="http://www.example.com/dir/file.js?lang=fr"></script>

  • basename (string) : Script filename (eg, "file.js").
  • domain (string) : Script domain (eg, "www.example.com").
  • dirname (string) : Script directory (eg, "/dir").
  • object : The DOM &lt;script&gt; element.
  • parameters (object) : An object containing the parameters specified in the query string part of the url. In our example, the "parameters" member would contain the following :

    { lang : "fr" }

  • path (string) : Url path, without query parameters (eg, "http://www.example.com/dir/file.js").
  • query (string) : Query parameters (eg, "?lang=fr").
  • url (string) : The whole script url (eg, "http://www.example.com/dir/file.js?lang=fr").
  • get ( name, defval ) : Retrieves the value of parameter "name". If undefined, "defval" will be returned.
  Files folder image Files  
File Role Description
Accessible without login Plain text file index.html Example Example page
Accessible without login Plain text file README.md Doc. Documentation
Plain text file thrak.script-1.0.1.js Class jQuery extension to retrieve script url parameters
Accessible without login Plain text file sample_include_1.js Example Example script 1
Accessible without login Plain text file sample_include_2.js Example Example script 1
Accessible without login Plain text file LICENSE Lic. License
Accessible without login Plain text file NOTICE Data Disclaimer

 Version Control Unique User Downloads Download Rankings  
 0%
Total:65
This week:0
All time:480
This week:6Up