1

have a small javscript that users can include into their sites like so:

<script src="http://www.mydomain.com/stuff.js?id=99" type="text/javascript"></script>

once included, this adds a clickable link to their page, all works good so far.

now for the part I am stuck on, in stuff.js we need to be able to use the value of id (99 in this case) in the functions

example:

destination = escape('http://mydomain.com/index.php?refferal=ID-VALUE-HERE');

basically just need to replace ID_VALUE_HERE with 99

Any ideas or tips how this relatively simple task could be accomplished?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Mark
  • 23
  • 4
  • edit or write your own stuff.js – vol7ron May 16 '12 at 23:59
  • I assume your being "funny"... the question is how to go about making such an edit? how to I access the value of ID? with only javascript, I obviously cannot rewrite the stuff.js file for every user. – Mark May 17 '12 at 00:04

2 Answers2

2

You can iterate over all script elements in the document, search for the one that loaded your script and extract any parameters from its attributes.

Yet, it would be much better to use (some small and really simple) server side processing to echo the url arguments directly into the delivered source code. Don't forget to escape them properly.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
2

1: Change your server config to interpret .js files as php files.
2: add to your .js file:

var id = "<?php echo !empty($_REQUEST['id']) ? $_REQUEST['id'] : '' ?>";

If you aren't able to rebind the file handling, you can also fix it using .htaccess and mod rewrite.

Sam Dufel
  • 17,560
  • 3
  • 48
  • 51
  • please use at least some quote escapes. – Bergi May 17 '12 at 00:04
  • thanks, this might be the easiest solution based on what I have read, was my first instinct but thought there must be a simpler solution using plain javascript. – Mark May 17 '12 at 00:07
  • Thanks Sam, got this working as needed. I'm not much of a javascript guy so this solution is very suitable for me. – Mark May 17 '12 at 00:25