Jump to content

Can anyone explain to me why some people append values to the src string..


monkeytooth

Recommended Posts

Can anyone explain to me why some people append values to the src string.. More often than not I will be digging through the source on something and I will see

<script type="text/javascript" src="somefile.js?abc=1234567&def=0998578394

 

 

in PHP and other languages I get that idea, but I cant figure out how/why people do that.. I get that it deals with something within the script its calling to but how would I use something like that, cause I've been wanting to build up an API thats shared, but exclusively on something I am working on, but that part of the equasion evades me, and I cant find anything in google on the idea..

 

so what is the point of:

?abc=1234567&def=0998578394

and how would I use it in my own coding?

Link to comment
Share on other sites

This isn't really a javascript question specifically...but anyways, the basic principle is that you can pass information in a parameter1=value1&parameter2=value2[etc...] to the target page - IOW pass arbitrary data.  Many times this is the only way to pass data to a target page, due to security or other code restrictions, for instance, passing data from one page to another where the pages are on different domains (cross-site scripting or XSS). 

 

As to why people do this...well there are virtually an unlimited number of reasons why you would want to pass data to a target page, though here are the most common reasons:

 

Pass information to help determine output

 

For example, lets say you have a website with 3 pages: home, about, contact us.  Now you can have 3 separate files with 3 separate URLs to link to in your site navigation bar, but instead of that, you can have a single URL that outputs the relevant content, based on a parameter you pass in the link. 

 

So for example, instead of

 

<a href='home.html'>home</a> <a href='about.html'>about</a> <a href='contact.html'>contact</a>

 

You can do

 

<a href='index.php?page=home'>home</a> <a href='index.php?page=about'>about</a> <a href='index.php?page=contact'>contact us</a>

 

Then in index.php you would have something like this (simplified for purpose of example):

 

<?php
$page = $_GET['page'];
include($page);
?>

 

Now based on this example, this doesn't seem like it's really worth doing...seems like it's just unnecessarily complicating things. But let's put this into perspective. 

 

Lets say that you have your 3 pages and you have opted to make them each their own individual page.  On  all 3 of your pages, you have a lot of the same exact code being used.  For the top of your page you have the same code for your site logo, links to your pages (nav bar), etc... You also have the same css/js output on all the pages (inline or as tags pointing to other files).  Maybe you also have the same code being put on all 3 of your pages for a footer area.  Maybe even the general structure of the actual body of your pages are the same, and the only thing really changing is some text in the body. 

 

The point here is that for all 3 of these pages, you have a lot of the same code being put on all pages.  So what happens if you need to do something like change the url of your logo?  You would have to go and make a change to all 3 of your pages individually.  Not so bad with 3 pages.  But let's say you have 10 pages.  Or 100.  Or 1000.  Now it sounds bad.

 

So the idea of having a single page is that you make a single "template" page with all the common stuff in it, and then you have separate files for each of your pages, but those files have content unique to that page (in reality you will more than likely actually store the unique page content as entries in a database).

 

Marketing Efforts

 

After you've spent the time and money and effort making your website, you want to advertise.  On the internet, this usually involves placing ads on other peoples' websites.  This can be in the form of paying to have your link show up on search engine search result pages, banner ads, etc... Since you are more than likely making an investment in getting people to display your links, you will usually want to be able to keep track of them, find out how many people are clicking on them and if they ultimately do whatever it is you want them to do (signup for something, register, purchase, etc...). 

 

This is called campaign tracking, and this is where web analytic tools come into play.  Tools like Google Analytics, Yahoo Web Analytics, Omniture SiteCatalyst, WebTrends, and many others that ultimately do the same thing in principle. 

 

So the principle is this:  Lets say you want to pay people to post a link to your site.  You pay for some keywords or ppc ad space on the major search engines and banner ad companies, to get some advertisement out there.  And lets say you have the link to your site displaying in 5 different places.  At each place, you are probably paying a different amount of money to have your link appear.  You're paying good money for that ad space and you don't want to keep investing in some place if it's not making you money or getting people to your site etc..

 

So on each link you would have a unique code like

 

http://www.yoursite.com/landingpage.html?source=google

http://www.yoursite.com/landingpage.html?source=bing

etc...

 

Then on all of the pages of your website, you will have tracking code in place.  And you have it look for that url parameter and report the value to the tracking tool.  And then you can go to the reports in your tracking tool and see how many people came to your site on each campaign code.  Also, you can setup your tracking tool's code to track actions on your site, like triggering other codes whenever the user does something, like complete a form or make a purchase.  Then the tracking tool can show you reports linking all these things together, so you can see for instance not only how many people came to your site from google, but how many of those people did certain things on your site (like actually purchase something). 

 

With this information you can then figure out which place to spend more money advertising on.  For example, if you see that 1000 people came to your site from one of your 5 campaign codes, but 800 of them were from one of them...that might be an indication to stop investing advertisement in the other 4 places.  I say "might" because you have to look at more than just how many people made it to your site.  If you keep digging and see you have 100 sales from those 1000 people, but all 95 of them came from those other 4 links (not the one that brought in the 800 people), then you can see that those other 4 links are actually a lot more valuable to you, because they produce higher conversion rates (people who complete whatever goal you want them to complete, like registering, purchasing, etc..). 

 

Campaign codes and campaign tracking is a bit more complex than this, but this is the core principle, extending to an entire ppc/seo/web analytics industry (and even then, each of those things are industries in their own right).

 

 

 

 

 

Link to comment
Share on other sites

All in all I get all that, as I think I mentioned in the original post, I understand it using PHP, but never understood it with JavaScript. Which is why I was targeting JavaScript in general, although your information is very insightful. To me its generally speaking known. Except for the logic as to why specificly with javascript one would do this.

 

I see it more often than not with Cross Site Scripts. Where you call specificly to a .js file and have the appended values after the js. To me as far as my knowlege of JS goes js doesn't read constraints like that with the URL grant it I could be horribly wrong maybe it does and I just don't know how as of the moment that works specificly. I have an idea in my head where I could use something like that but it would apply to specificly php, and in my case what I am intent on building isnt solely php based. Grant it the script I am building for what I want to do is heavy AJAX on the backend elements. But still none the less passing the variables ie:

 

myscript.js?var1=hello&var2=welcome

 

Doesnt make sense to me as I said I don't know of any straight out way to grab those values with javascript I can't think or find anything thats equivilant to php's _get function for javascript.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.