tags:

views:

32

answers:

4

How can I access modules in my js file:

<script type="text/javascript" src="script.js?modules=test,cookie"></script>
+1  A: 

script.js should be a file with server side logic that sends js modules as response according to the input. But i don't recommend this approach (you miss caching).

Place your modules into separate files.

(or merge them for faster download)

galambalazs
+1  A: 

a super hacky way to do it would be to give your script tag an id attribute and then access it from within the script itself, pulling out and parsing the src attribute :)

<script id="myscript" src="script.js?modules=a,b"></script>

in jquery you could do:

$(function(){
    var modules = $('#myscript').attr('src').split('=')[1].split(',');
});

like i said, super hacky, but could work!

Jason
+1  A: 

Assuming you mean:

How can I read the query string used to load my JS file in my JS?

Then the most reliable way is to generate the JS server side and include the data that way.

For example:

<?php
    header('Content-Type: application/javascript');
    echo 'var modules = ' . json_encode($_GET['modules']);
?>

A less reliable, but entirely client side, method is to find the last script element in the document and parse the URI.

var scripts = document.getElementsByTagName('script');
var myScript = scripts[scripts.length - 1];
var uri = myScript.src;
// and so on
David Dorward
+2  A: 

give an ID to your script tag

<script type="text/javascript" src="script.js?modules=test,cookie" id='YOURID'></script>

then in your js file use :

jsSrc  = document.getElementById("YOURID").src;
var jsQry = jsSrc.substr(jsSrc.indexOf("?"));
rahim asgari
Exactly what I was looking for, thx for all your responses guys
Cedric Dugas