By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,519 Members | 1,235 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,519 IT Pros & Developers. It's quick & easy.

Can you dynamically add a ? query string to a URL?

P: 8
Hello,

I'm trying to append the query string in the URL from a previous page to the current page's URL. The original query is brought over from Google AdWords and my code determines if the URL contains a query. If it does contain a query, a new phone number is displayed to the visitor so that we can better track the performance of our keywords. Another part of the code determines if the previous URL had a query string and executes the same code to show a new phone number. However, if the user clicks on a third page, the code becomes useless. How can I add the query string to the current URL so the code below continues to work throughout?

The code:


Expand|Select|Wrap|Line Numbers
  1. var q = unescape(location.search.substr(1)).split('&');
  2.  
  3. if(document.URL.indexOf("?") > 0){
  4.  
  5.  
  6. document.write("<IMG SRC='callustemplateadwords.jpg'>")
  7.  
  8. }else{
  9.  
  10.  
  11. if(document.referrer.indexOf("?") > 0){
  12.  
  13.  
  14. document.write("<IMG SRC='callustemplateadwords.jpg'>")
  15.  
  16.  
  17. }else{
  18.  
  19.  
  20. document.write("<IMG SRC='callus.jpg'>")
  21.  
  22.  
  23.  
  24.  
  25.     } 
  26. }
Thanks!

Chris
Jun 9 '08 #1
Share this Question
Share on Google+
7 Replies


acoder
Expert Mod 15k+
P: 16,027
Add the query string to the location.href:
Expand|Select|Wrap|Line Numbers
  1. location.href = "someurl.php?query=val";
Jun 9 '08 #2

P: 8
Add the query string to the location.href:
Expand|Select|Wrap|Line Numbers
  1. location.href = "someurl.php?query=val";
Thanks for your reply. I need to make the addition of the query dymanic.

So something like location.href = (document.URL) + the ?query of the previous URL

That's obviously not real code, but that's my thought process so far.

So if the previous page was http://www.website.com/?g=[keyword]

The next page would take on the ?g=[keyword] of this page.
Jun 9 '08 #3

acoder
Expert Mod 15k+
P: 16,027
location.search is what you're looking for.

Add it to the new URL:
Expand|Select|Wrap|Line Numbers
  1. location.href = "newurl.php" + location.search;
Jun 9 '08 #4

P: 8
location.search is what you're looking for.

Add it to the new URL:
Expand|Select|Wrap|Line Numbers
  1. location.href = "newurl.php" + location.search;

Thanks!

Is the "newurl.php" the URL of the current page? This is centralized code that is called on every page, so I would like it to pull the current URL and update it with the search of the previous URL. Will the code you provided do this?
Jun 10 '08 #5

acoder
Expert Mod 15k+
P: 16,027
"newurl.php" was just an example.

The current is in location.href and you you could get the previous URL using document.referrer. Can you give an example scenario?
Jun 10 '08 #6

P: 8
"newurl.php" was just an example.

The current is in location.href and you you could get the previous URL using document.referrer. Can you give an example scenario?
Sure thing, step by step:

A user clicks on a Google Adword for us:

They hit the landing page for that ad and Google adds a ?g=some info

So, the url for an organic or direct visit would be www.mysite.com/products
The url from a Google Adword click would read www.mysite.com/products?g=some info

My current code says if the url has a ? then show phone number A
or if the previous url had a ? then show phone number A
otherwise show phone number B

This works on the landing page from a Goodle Ad and for 1 page after that. The code stops working after the user navigates 2 pages away from the landing page.

The code I want to build would copy the ?g=something from the landing page and add to every url for that users session, creating an infinite environment for my current code to function.

So, if user came to the site organically, url is normal and phone number B (non-PPC) is shown throughout their session on every page.

If the user came from a Google Ad, the end of the url would contain a ?g=some info, that info would be added to every page following
Jun 10 '08 #7

acoder
Expert Mod 15k+
P: 16,027
You can control every link on your page by adding adding location.search to the URL. Alternatively, you can add this using server-side code which loads the page with all URLs automatically having the search words.

If you are going to do this via JavaScript, you can call a function onload which goes through all the links in the page, e.g.
Expand|Select|Wrap|Line Numbers
  1. var links = document.getElementsByTagName("a");
  2. for (i =0; i < links.length; i++) {
  3.     a[i].href += location.search;
  4. }
Jun 11 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.