Solved

Adding javascript to a php page

Posted on 2011-09-13
7
329 Views
Last Modified: 2012-06-21
I am new to web design.  I have a php page at www.mauitradewinds.com/TripAdvisor.php and it redirects traffic to another site when visitors click a certain button on my navigation menu.  I would like to track the usage of that button, so I inserted javascript tracking code, but it isn't working.  Please advise. TripAdvisor.php
0
Comment
Question by:ddantes
  • 4
  • 2
7 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 250 total points
ID: 36533878
There's an order in which things occur in PHP pages. Before any Javascript and HTML are sent to the visitor's computer, the PHP is executed first. So in your code, you have PHP code that redirects to another site (the "header" line). This redirect occurs before any of your Javascript tracking code is seen by the browser, so the browser doesn't run that Javascript code.

You could always remove the PHP redirect and then use the window.location Javascript command to perform the same redirect:

<script type="text/javascript">
<!--
window.location = "http....Hawaii.html";
-->
</script>
<noscript>
      <a href="http....Hawaii.html">Click here if you are not automatically redirected.</a>
</noscript>
0
 

Author Comment

by:ddantes
ID: 36534118
Thank you, that is a very nice recommendation.  I will accept that solution after verifying that the revised redirect page is being tracked.
0
 

Author Comment

by:ddantes
ID: 36534466
I don't think the new page is tracking.  I placed the javascript for the tracking after the javascript for the redirect.  Is that placement critical?   The revised redirect page is at www.mauitradewinds.com/TripAdvisor.htm   
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:ddantes
ID: 36535013
I tried placing the tracking script before the redirect script.  Either way, the page is not being tracked.  Can you please have another look?
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 36537015
Can you show the code for that page now?
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36537445
Here it the general design I would use (no JavaScript here).

Make the link in your navigation go to one of your pages.  Inside that page, write a row into a data base table with the DATETIME, and the client IP address.  Then use header("Location") to redirect to the foreign URL.
0
 

Author Comment

by:ddantes
ID: 36537582
Thank you both for your comments.  gr8gonzo:  I was mistaken, and the page is tracking, now that I placed the javascript before the redirect code.  Thank you for your help!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question