0

I see much scripts in traffic exchange services or affiliate programs like the following

<script type="text/javascript" src="http://trafficexchange.com/trafficout/user/300?limit=10"></script>

A source without file extension. How does this work? Is this a javascript file or php file? If this is only javascript how can they access the database to collect stats about who clicked on your link etc (AJAX?)?

The query variable limit=10. Again, is this javascript or php? If it is javascript, how can you access this variable?

Other example from Google Ads

<script type="text/javascript">
    google_ad_client = "xxxxxxx";
    google_ad_slot = "xxxxx";
    google_ad_width = 336;
    google_ad_height = 280;
</script>

<script type="text/javascript"
src="//pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

Again, how can google access their database to display relevant ads with only a .js file?

7
  • 4
    It's (probably) a dynamically generated JavaScript code. JS is just text, so you could for example write var x = <?php echo $_GET['limit']; ?>; and the browsers sees it as var x=10; which is valid JS syntax. Commented Feb 11, 2015 at 11:01
  • That is clear to me that you can add a .php file as the script source and on the backend the php is executed with a header('Content-Type: application/javascript'); to tell that you are outputting a javascript file. But what when there is no file extension in the source file ? Commented Feb 11, 2015 at 11:14
  • 1
    You don't need a file extension. The type of the script is defined in its type attribute. The src does not need to be a valid file, it just has to be a valid URL. Commented Feb 11, 2015 at 11:28
  • 1
    With URLs and HTTP the Suffix is a weak indicator for the file type. The clients use Content-Type or the context to determine the file type. The server is free to respond with what ever Content-Type it likes for a requested url (it just needs to take care that it will match a Content-Type the client would like to have). The server gets a request and if the there is a route defined to create/load a JS file when trafficout/user/300 is requested, then the server will create the JS file responding with a application/javascript content type. Commented Feb 11, 2015 at 11:31
  • I don't understand why this question gets so much hate. It's a valid, answerable question. Commented Feb 11, 2015 at 11:42

2 Answers 2

1

The client only expects one thing of an included <script> file: to have the proper Content-type: application/javascript header.

So a PHP file like this is valid (although bad form):

<?php
header('Content-type: application/javascript');
?>

var answerToEverything = <?php echo 42; ?>;

And the browser will see it as a valid JavaScript file containing

var answerToEverything = 42;

Even if the file is not explicitly named .js.

A more general approach is to capture all requests to your webserver, do some action, and then include your JavaScript file with the proper header.

Note that including PHP variables directly inside JavaScript files is considered bad practice.

For more information see How to pass variables and data from PHP to JavaScript?

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

Comments

0

Scripts like this are probably being run by PHP (or similar server-side scripting language).

Doing so is a case of, on an apache server, adding something similar to the following in the .htaccess file:

RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^trafficout/user/(\d+)$ some-script.php?%1&user=$1 [L]

What this is doing is looking for any requests for the above file, then passing in the query string ((.*)) and user id ((\d+)) into a php script.

What are %1 and $1 doing?

In a htaccess file %1 and $1 represent variables retrieved from RewriteConditions and RewriteRules. Specifically, %n represents any variables acquired from a %{QUERY_STRING} condition and $n repesents any variables acquired from rules. In both cases n represents the variable id. In the example above, there is one query string variable and one rewrite rule variable so both are 1.

For the script in your question, this will turn the second part of our rewrite rule into:

some-script.php?limit=10&user=300

How we get the variables from the query string etc is done via Regular Expressions, and is a WHOLE other topic that I am not going to go into right now.

The PHP script will then get the variables using $_GET or similar, for example:

$user = $_GET['user']; //bad example - no validation etc.

[L]?

This is a flag used to tell the server to stop processing any more rewrite rules.

No file extension

It is possible to call for a file without a file extension, as long as the script sets the correct content-type header, then the browser will process the returned file. In PHP, this would be done like:

<?php
header('Content-type: application/javascript');

And is sent before any content.

Google Ads

The original .js script is just a .js script. What the script is doing is generating an ajax call back to the Google Servers replacing local variables with whatever variables you have defined.

2 Comments

I am sorry, but the majority of your answer does not answer the given question here - This wasn't a question about htaccess rewrite rules. I would recommend deleting basically everything until "No file extension". Also, this stuff is misleading, as it is specific to Apache and there are lots of other web servers out there. I would also like to stress or similar server-side scripting language as there is no reason this has to be PHP and I would guess the majority of these services in fact do not use PHP
To summarize: The file in the javascript source attrribute can be a form of url rewriting. The url converts to a php file which outputs javascript. In the case of Google Ads, AJAX is used. Ok, now it looks simple. Never knew why someone would use clean url's for a javascript attribute. Is this for SEO reasons or security ?

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.