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?

2
  • edit or write your own stuff.js Commented May 16, 2012 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. Commented May 17, 2012 at 0:04

2 Answers 2

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.

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

3 Comments

please use at least some quote escapes.
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.
Thanks Sam, got this working as needed. I'm not much of a javascript guy so this solution is very suitable for me.
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.

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.