0

wondering how I can pass a variable from load js like:

<script type="text/javascript" src="script.js?myVar=myValue"></script>

and use and pass to script.js itself? I Know about declare variable before, but I'm looking for url way.

Thanks in advance.

4 Answers 4

3

The javascript wont have access to that value. The server would have to look for that and insert that value into the rendered javascript on your behalf.

Usually though, the pattern is more like this:

<script type="text/javascript" src="script.js"></script>
<script type="text/javascript">
  ObjFromScript.myVar = 'myValue';
  ObjFromScript.doStuff();
</script>
Sign up to request clarification or add additional context in comments.

Comments

2

You'd have to locate the <script> tag in the document and extract the arguments. A prominent example of a site using this is facebook with the #xfbml=1 hashtag.

However, the proper/nice way is not putting any code but functions in script.js and then call someFunction(myValue);.

3 Comments

How does FaceBook extract it from the URL? With JavaScript?
Yes, with their script embedded in one's site they obviously have access to this site's DOM tree. And since they know the URL of their script they can simply search for a script tag pointing to it and then extract the argument from its src attribute.
Oh yes, it's so obvious when I actually use my head :)
1

You could do it on the client or the server. On the client, get hold of the script element and parse the src attribute.

<script id="myScript" src="script.js?myVar=foo"></script>
<script>
    var s = document.getElementById('myScript');
    s.src; // parse it and extract myVar
</script>

On the server, setup routes so that script.js goes to some handler written in a server-side language like PHP. In that case you could be outputting script.js dynamically. Here's an example in PHP.

script.js => script.php

<?php 
    header('Content-Type: application/javascript');
    $myVar = $_GET['myVar'];
?>

// optionally expose myVar inside JavaScript
var myVar = <?php json_encode($myVar) ?>;

// regular JavaScript
alert(myVar);
var x = 10;

Comments

0

You could use document.currentScript, but it isn't widely supported.

var matches = document.currentScript.src.match(/\?myVar=([^&]+)/),
    myVarParam = matches && matches[1];

Alternatively, you could try getting the script element with the matching URL and parse out the GET param.

var scriptPath = 'path/to/this/script.js',
    matches = $('script[src^="' + scriptPath + '"]').attr('src').match(/\?myVar=([^&]+)/),
    myVarParam = matches && matches[1];

6 Comments

:last? You cannot know if the script tag of the loaded script is the last one. So you need to iterate and - unless you don't want it to be portable - check the ´src` to match the location/name of the script file (which needs to be hardcoded in the script).
@Thief I was thinking because the scripts are executed synchronously - so long as this code runs straight away in the linked file. Maybe I'm wrong, just trying to think up a solution :)
Either way, this approach strikes me as a clever hack that allows you to do it wrong. I wouldn't encourage it, personally.
The DOM tree is already fully loaded so all script tags are present - even though not all scripts might have been loaded yet.
@Thief @Squeegy Yeah, I think it was wishful thinking :) I've modified my answer.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.