Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If URL Contains 'affiliate1' Then... For example: www.MyUncleVinny.com

Status
Not open for further replies.

yashax

IS-IT--Management
Oct 15, 2010
6
US
I have a website We use affiliate codes for financial reporting. For example:
How can I write a simple JavaScript function that will look at the URL and determine if it contains "affiliate1" then do something, like display image1, if the URL contains "affiliate2", then display image2, else display image3.

Thank you SO MUCH in advance.
 
Something like:

Code:
var affCode = window.location.search.substr(1);

switch(affCode) {
   case 'affiliate1':
      // Do something here for affiliate 1
      break;
   case 'affiliate2':
      // Do something here for affiliate 2
      break;
   default:
      // Do something here for no affiliate / unknown affiliate
}

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
1. You need to put your script in the <head> section
2. You need to cause the code to be run (eg <body onload>
3. The code needs to actually do something, like write to the document, or change the contents of a div.
A simple write to replace the contents of a div looks like
Code:
<head>
<style type="text/css" media="screen">
    * {margin: 0; padding:0;}
</style>

<script type="text/javascript">
function startup(){
var affCode = window.location.search.substr(1);
var target=document.getElementById("picture");
switch(affCode) {
   case 'affiliate1':
target.innerHTML=("<div><a href='[URL unfurl="true"]http://hies.org'[/URL] target='_blank'><img src='[URL unfurl="true"]http://www.thedealnation.com/myunclevinny.com/images/hollyinnocents.jpg'[/URL] alt='I support this charity' style='border: 2px solid #66945c;' /></a></div>");
break;

   case 'affiliate2':
target.innerHTML=("<div><a href='[URL unfurl="true"]http://www.herobox.org'[/URL] target='_blank'><img src='[URL unfurl="true"]http://www.thedealnation.com/myunclevinny.com/images/charity.jpg'[/URL] alt='I support this charity' style='border: 2px solid #66945c;' /></a></div>");
break;
   default:
target.innerHTML=("<div><a href='[URL unfurl="true"]http://www.herobox.org'[/URL] target='_blank'><img src='[URL unfurl="true"]http://www.thedealnation.com/myunclevinny.com/images/charity.jpg'[/URL] alt='I support this charity' style='border: 2px solid #66945c;' /></a></div>");
break;
}}
</script>
</head>
<body onload=startup()>
<div id="title">
<h1>This is the header</h1>
</div>
<div id="picture"></div>
</body>

You will obviously have to write the rest of the page, making sure you have a complete and valid doctype to start with, but this shows the kernel of your requirements.

If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Thank you so much for your help. I have a slight dilemma. Based on your suggestion I made modification and it works(you can try to change affiliate1, 2,..):
I use this file, charity.html as an Include for the main website at The code does not work when it acts as an include. Do you know why?
For example:
Ideally, I must get this working only by making changes to charity.html and NOT the main website.
 
Do a 'view source' on the page you are pointing to. I can see no trace of the code or any mechanism for running it. Without knowing what your 'include' is or does it is difficult to advise further. I note that the site uses jquery, so maybe you can add the code to the $(document).ready(function() section.

If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
If the include file is not being included correctly (which clearly it isn't if the code isn't showing up when viewing the page source), then obviously it will never run.

The fact it is not being included is not a question for this forum, but one for whichever web server you are running.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
The of course it won't work - you're not passing the parameters through to the URL of the iframe anywhere.

Why run the code in an iframe? Why not simply include it as a piece of JavaScript as that is all it is?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
Great question. I do not control the website, but only the charity.HTML, that is why I am hosting it, to get full control. Is there anything I can do, in order to accomplish what I want? Can the main site be modified only one time with some code to give me full control of charity.html, so I can do what I want? If so, please be kind to let me know what it is and how to make it work.

Thank you so very much for your assistance.
 
If you have the ability to modify the main website - which it sounds like you do - then include the JavaScript in a <script> element instead of static HTML within the <iframe>.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top