Recommend this page to a friend! |
Download .zip |
Info | Example | View files (7) | Download .zip | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2015-11-01 (0 years ago) | Not enough user ratings | Total: 63 | All time: 439 This week: 37 |
Version | License | JavaScript version | Categories | |||
thrak-script 1.0.1 | BSD License | 1.0 | jQuery, Language |
Ever wanted to pass url parameters to your scripts using the <script> 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().
There are two contexts you need to consider for using script.js :
In your html page, you first need :
A reference to jQuery, for example :
<script language='javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js'></script>
A reference to script.js :
<script language="javascript" type="text/javascript" src="thrak.script-1.0.1.js"></script>
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>
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 :
The following paragraphs give some various "techniques" to save this data.
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.
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 ( ) ) ) ;
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>
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" }
Files |
File | Role | Description |
---|---|---|
index.html | Example | Example page |
README.md | Doc. | Documentation |
thrak.script-1.0.1.js | Class | jQuery extension to retrieve script url parameters |
sample_include_1.js | Example | Example script 1 |
sample_include_2.js | Example | Example script 1 |
LICENSE | Lic. | License |
NOTICE | Data | Disclaimer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
0% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.