Forum Moderators: coopster

Message Too Old, No Replies

How to get a java-script into a .php file

         

Jettie

8:42 am on Feb 26, 2004 (gmt 0)

10+ Year Member



I own a classifed ad site and I have to put some javascritps into it.
One is for the statistics and the other is an Adsense code.
The Adsense codes are not alowed on search result and welcome pages, so I can't put it in the header, because then, they show up on EVERY page.
I have to put the java script codes on the detail pages, wich are all .php files.
I'm a php-dummie, so can anyone help me out here?

mykel79

10:58 am on Feb 26, 2004 (gmt 0)

10+ Year Member



What PHP does is parse the code on the server and print out normal HTML that the Internet user receives. You can even set the server so that the user doesn't know they're watching a page made in PHP.
So what you have to do is go through the PHP file and ad a line that prints out the javascript.
Going through all the php detail files might be tiresome, so you could try putting it in the header in an IF statement.
Something like this:

IF ($detail_page==1) echo <<<END
<script>
this is some script code
</script>
END;

Now in the detail pages you add a line
$detail_page=1;
before the line that includes the header. It's a one time operation, and the next time you want to modify the javascript you can just change it in the header.
So what does this do? If the header is being included in e.g. a welcome page, the javascript won't be shown. If it's being included in a detail page, the javascript will be shown.

someguy

2:17 pm on Feb 27, 2004 (gmt 0)

10+ Year Member



i am assuming that you mean, you have a few pages but they all use a common header/footer and you dont want the javascript to appear on all the pages, just specific ones.

Well in that case its quite simple.

Save your javascript into a file that has a JS extension.
Then in the files you DO want the javascript, put this code.

<?
include("myFilename.JS");
?>

Now, this can be used in any number of ways, for instance, if you where using form or URL variables
across pages, you could create a function that checks
to see if a certain variable is present and then the function woould include the Javascript or not depending on said variable.

cheers

Jettie

9:53 am on Feb 28, 2004 (gmt 0)

10+ Year Member



Thanx all ... You helped me out a lot!
I will give it a try ...