How to Reference External .JS file within PHP code

kvakil
kvakil used Ask the Experts™
on
Hi,

I have to insert a javascript function into my php code.  Currently, it is hard coded within the php file but I would like to externalize the javascript code.  Can someone tell me how to reference a .JS file within PHP?  

Thanks,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
It's actually just like adding it to any HTML file, you just do it outside of a PHP block.

<?php
//Your PHP code goes here like normal
?>
<script src="somescript.js" type="text/javascript"></script>
<?php
//You can do more PHP here if you like
?>

Or if you want to do it within the PHP block;

<?php
echo('<script src="somescript.js" type="text/javascript"></script>');
?>

Author

Commented:
Would this look right?  
 <div class="content nofloat">
<?php
echo('<script src="http://www.mysmp.com/sites/all/themes/mysmp/js/investing_channel_125_125.js"
type="text/javascript"></script>');
?>
<?php print $content ?>
  </div>

See code within the .js below.  Is this going to work?  The premise is to left align an advertisement within the body of my content...
<div style = "float:left; padding-right: 5px;"> 
<!-- begin ad tag (tile=4) -->
<script language="JavaScript" type="text/javascript">;
document.write('<script language="JavaScript" src="http://ad.doubleclick.net/adj/invc.mysmp/homepage;kw=;kval=homepage;tile=4;sz=125x125;ord=' + ord + '?" type="text/javascript"><\/script>');
</script><noscript><a href="http://ad.doubleclick.net/jump/invc.mysmp/homepage;kw=;kval=homepage;tile=4;sz=125x125;ord=123456789?" target="_blank"><img src="http://ad.doubleclick.net/ad/invc.mysmp/homepage;kw=;kval=homepage;tile=4;sz=125x125;ord=123456789?" width="125" height="125" border="0" alt=""></a></noscript>
<!-- End ad tag --></div>

Open in new window

Commented:
That's actually not going to work, no. The contents of the JS file you have there are not actually JavaScript, though they do contain some JavaScript. Here's what I'd probably recommend. Note that I created a variable for "ord" in the JavaScript because it was being called without being defined. You can massage it as necessary.

Also, I just kind of combined the two files. Easier this way, I think.
<div class="content nofloat">
	<div style = "float:left; padding-right: 5px;"> 
		<!-- begin ad tag (tile=4) -->
		<script language="JavaScript" type="text/javascript">
			var ord = "123456789";
			document.write('<script language="JavaScript" src="http://ad.doubleclick.net/adj/invc.mysmp/homepage;kw=;kval=homepage;tile=4;sz=125x125;ord="+ord+"?" type="text/javascript"><\/script>');
		</script>
		<noscript>
			<a href="http://ad.doubleclick.net/jump/invc.mysmp/homepage;kw=;kval=homepage;tile=4;sz=125x125;ord=123456789?" target="_blank"><img src="http://ad.doubleclick.net/ad/invc.mysmp/homepage;kw=;kval=homepage;tile=4;sz=125x125;ord=123456789?" width="125" height="125" border="0" alt=""></a>
		</noscript>
		<!-- End ad tag -->
	</div>
	<?php print $content ?>
</div>

Open in new window

Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Hi, thank you for your response.

I actually need to get everything above " <?php print $content ?>" out of the php file and into an external file.  Will this do it?  Are you suggesting that I put all of that in the external .js and reference or are you saying that a reference is not possible?  thank you again

Commented:
What you could do is move everything into a separate file like "investing_channel_125_125.html", then call it as follows.
===== PAGE investing_channel_125_125.html =====
<div style = "float:left; padding-right: 5px;"> 
	<!-- begin ad tag (tile=4) -->
	<script language="JavaScript" type="text/javascript">
		var ord = "123456789";
		document.write('<script language="JavaScript" src="http://ad.doubleclick.net/adj/invc.mysmp/homepage;kw=;kval=homepage;tile=4;sz=125x125;ord="+ord+"?" type="text/javascript"><\/script>');
	</script>
	<noscript>
		<a href="http://ad.doubleclick.net/jump/invc.mysmp/homepage;kw=;kval=homepage;tile=4;sz=125x125;ord=123456789?" target="_blank"><img src="http://ad.doubleclick.net/ad/invc.mysmp/homepage;kw=;kval=homepage;tile=4;sz=125x125;ord=123456789?" width="125" height="125" border="0" alt=""></a>
	</noscript>
	<!-- End ad tag -->
</div>
 
 
===== YOUR PHP FILE =====
<div class="content nofloat">
	<?php 
            include '/sites/all/themes/mysmp/js/investing_channel_125_125.html';
            print $content;
         ?>
</div>

Open in new window

Commented:
May need a full URL to the file or to make it relative to the directory your PHP file is in. Not sure where you're running this from, but some tinkering with it should produce the desired results.

Commented:
er, meant to say full path to the file, not full URL

Commented:
I'm leaving work now. Feel free to post any other questions and I'll see if I can check back later tonight. Hope I've been able to help :)

Author

Commented:
hmm...I appreciate all the thoughts.  I tried what you suggested and it did not work.  Im sure some tweaking is required.  I  tried both the relative path and the full path....
Commented:
We'll just go back to your JS example and make that work. Here you go.
===== PAGE investing_channel_125_125.js =====
document.write('<div style = "float:left; padding-right: 5px;">';
var ord = "123456789";
document.write('<script language="JavaScript" src="http://ad.doubleclick.net/adj/invc.mysmp/homepage;kw=;kval=homepage;tile=4;sz=125x125;ord="+ord+"?" type="text/javascript"><\/script>');
document.write('<noscript><a href="http://ad.doubleclick.net/jump/invc.mysmp/homepage;kw=;kval=homepage;tile=4;sz=125x125;ord=123456789?" target="_blank"><img src="http://ad.doubleclick.net/ad/invc.mysmp/homepage;kw=;kval=homepage;tile=4;sz=125x125;ord=123456789?" width="125" height="125" border="0" alt=""></a></noscript></div>');
 
 
===== YOUR PHP FILE =====
<div class="content nofloat">
	<script src="test.js" type="text/javascript" language="javascript"></script>
	<?php print $content ?>
</div>

Open in new window

Commented:
Er, replacing "test.js" above with the location of your JS file of course.

Author

Commented:
thanks again, still no luck...

Commented:
No luck in what way? I have been able to successfully test it locally.

Author

Commented:
Well, when I implement that solution, the ad does not appear in the body of the content.....

Author

Commented:
FYI...I put the full URL path of the file in the php file.

Commented:
I've been able to see the ads. Sometimes there is a blank ad in the rotation, but that appears to be the advertising host, not the client-side script.

Author

Commented:
Right...that is true with the ads

Is it possible there is something with drupal that is causing this?

Commented:
The JS is missing a closing parenthesis.


The JS is missing a closing parenthesis. My bad.

document.write('<div style = "float:left; padding-right: 5px;">');

Author

Commented:
hmmm...did that..no luck either....

Author

Commented:
I take that back, I put that close bracket in the wrong place.  It works now!  Thanks so much!!

Commented:
Outstanding! I knew we'd get there :)

Author

Commented:
Hi Teggert,

Can you view this page in IE again?  I think the ad is not wrapping.  I am not sure why I didnt see this before.  It might be something with the cache on my computer?

http://www.mysmp.com/technical-analysis/tradestation.html

Author

Commented:
very responsive and knowledgeable at the same time.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial