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
161k27 gold badges264 silver badges311 bronze badges
asked May 16, 2012 at 23:57
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.

answered May 17, 2012 at 0:01
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.

answered May 17, 2012 at 0:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.