7

If i have a javascript src like

<script src="http://domain.com/js/script.js"> 

Could i add some more values to the end of it

<script src="http://domain.com/js/script.js?state=1"> 

And get state inside that javascript ??

This idea came from a situation that i have to get client resolution and save it to a session but i can not take it by php , so i have to get it by javascript, but i dont want to show the script nuded from the source code

I think it is possible because i saw many of sources do have link like this!

10
  • 1
    Why do you have to pass the resolution to the script? Can't the script calculate it when needed? Commented Jul 8, 2013 at 16:31
  • I don't understand what you mean. What would state do? Commented Jul 8, 2013 at 16:31
  • This is possible, see Passing JavaScript arguments via the src attribute for details Commented Jul 8, 2013 at 16:32
  • 1
    Any time I have added a query string parameter to a script source it has been a time-stamp or version to prevent caching. Commented Jul 8, 2013 at 16:32
  • When I need to give parameters, i normally make a PHP file (header as text/javascript) which $_GET the values and outputs the formatted js file : <script src="http://domain.com/js/script.php?state=1" />. But the ? values on a js is what Jeremy said, of timestamp or version, to prevent cache. Commented Jul 8, 2013 at 16:33

3 Answers 3

12

just in case someone wants the actual answer to the actual question instead of resorting to global variables and two script tags:

<script src="http://example.com/js/script.js?state=1"></script>

inside script.js:

(function(){
  var myTags = document.getElementsByTagName("script");
  var src = myTags[myTags.length-1].src;
  var state = unescape(src).split("state=")[1].split("&")[0];
  alert(state);
}());

if you need to pass a lot of stuff, use a common queryString parser to turn the script's queryString into an object.

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

3 Comments

I know this is two years old, but it works perfectly with my 1-line widget include that I am building. It requires a user ID which I can now pass without cluttering up the global namespace. +1
@Cluckles: know that newer browsers support the document.currentScript property, which makes the "guts" of the function boil down to state=unescape(document.currentScript.src).split("state=")[1].split("&")[0]
Thank you @dandavis a shorter version is: var src = document.currentScript.src; var url = new URL(src); url.searchParams.get('state');
4

Cant you do something like this?

<script>
    var state = 1;
</script>
<script src="http://domain.com/js/script.js"> 

2 Comments

Ah, thanks! How can i forget this !!!
I agree with dandavis. This does not answer the question. Sure, it solves their problem in their case. But the question they asked was something different.
1

Give variable before calling JS will work. But if you don't want to pollute Global with vars, you can try the PHP approach:

Make a PHP file declaring in the header it will be a javascript, do whatever you want with the variable grabbing it with $_GET the values and outputs the formatted js file with

<script src="http://domain.com/js/script.php?state=1" />

<?php
// We'll be outputting a JS
header('Content-type: text/javascript');

$phpState = $_GET['state'];
?>

function someCommonJS(){
    var myState = "<?=$phpState;?>"; /*printing php var into js*/
}

<?php
/* some more php*/
?>

/*some more js*/

This way the file comes as you want, without user being able to see all your if else that would be visible if done via js.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.