0

Imagine I have a script abc.js, which has one global variable userid. Several AJAX requests are made instantly after it is loaded (it can happen after or before DOM is parsed by browser). I need the script to have different variable value for different users. Requests has to be made instantaneously after JS is available. Here is what I am looking for:

  • Browser makes a request to /js/abc.js?userid=12345
  • Server parses userid=12345 and adds userid = 12345 to the script
  • Server returns the script to user
  • Browser parses the script and userid is set to 12345, then everything is processed normally.

Is there a way to do this with some Apache/Lighttpd extension? Since I can't rely on <script>userid=12345</script> at the begining of the document, as abc.js will most probably be executed before userid=12345 is evaluated.

4
  • You can set your script tags src attribute to a php file, and echo the javascript Commented Dec 24, 2013 at 18:29
  • 1
    I don't follow the logic here. If the first step is calling /js/abc.js?userid=12345 then you obviously already have the userid available... Commented Dec 24, 2013 at 18:29
  • The logic was like calling /js/abc.js.php which is basicly a php file prints javascript codes, yent's answer is better and php interpreter won't have to handle javascript requests too Commented Dec 24, 2013 at 18:39
  • @DigitalChris but JS does not. I have a page profile.php and I know user ID (I use it to load user name and info), however I use Ajax to pull additional data, like comments, and I need to pass the id with Ajax reqeusts. To do that JS needs to have the id.. Unless.. I just got an idea, I'll try it now. Commented Dec 24, 2013 at 19:10

2 Answers 2

1

You can simply use PHP to output the javascript variables at a point before when the abc.js script is called. In this case the parameter would be passed to the main PHP script (i.e the page). Like this:

<script type="text/javascript">
var userid = <?php echo $_GET['userid']; ?>;
</script>
<script type="text/javascript" src="/js/abc.js">

Of course you probably want to sanitize the parameter value in PHP before using it.

Here, abc.js can just reference the value of userid in global scope. There is no need to pass userid to the script in URL and have Apache use PHP to dynamically generate the js file. In fact, you likely don't want that approach as it prevents optimal browser caching of the script.

If you don't want PHP involved you have other options to pass this data such as the URI parsing option presented by @yent, HTML5 localStorage, cookies, etc.

Sign up to request clarification or add additional context in comments.

2 Comments

Yep, this worked. I was doing exactly this, but I had them swapped around: external JS came before the setting of userid.
For safety and versatility, I would recommend using json_encode to pass data between Javascript and PHP. I have an answer here that details how to do that: stackoverflow.com/questions/9136997/…
0

You can parse window.location.search and set window properties :

var p = window.location.search.substr(1).split(/&/);
if(p.length && p[0]) for(var i=0; i<p.length; i++) {
    var a = p[i].split(/=/);
    window[a[0]] = a[1];
}

Comments

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.