Link to home
Start Free TrialLog in
Avatar of ellandrd
ellandrdFlag for Ireland

asked on

pass value to external javascript file

i place all my javascripts in an external file.  it is possible to pass values to functions?

i set some variables using PHP that i want to pass to my external javascript file.

i have tried this but didnt work:

<?php
$navigationHeight = 360;
?>
<script type="text/javascript" src="/scripts/global.js?navigationHeight=<?php echo $navigationHeight; ?>"></script>

in my global.js file i done this:

resizeDivHeight.custom(0,location.search);

please advise
ASKER CERTIFIED SOLUTION
Avatar of Joe Wu
Joe Wu
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ellandrd

ASKER

yes i understand what you are saying.  im not sure if it will work so give me a few mins to try your idea out...
your idea works perfect.  why didnt i think of that... it was sitting looking at me but i had "passing the URL values" to the JS file in my head since earlier in work and when i came home,  my mind was messed up...

thanks for the knock in the head and making me see sense.

ellandrd
glad you got it working :)
>>it was sitting looking at me but i had "passing the URL values" to the JS file...

This often comes with too much PHP scripting....
solution
// i set some values in my PHP page - the 360 is just an example that is fit for purpose...
<?php $navigationHeight = 360; ?>
 
// this is how i pass the PHP values into my Javascript function
<div id="banner"><a href="javascript:void(0);" id="toggle" onclick="javascript:toggleNavigationBar(<?php echo $navigationHeight; ?>);return false;" title="Click here to toggle my navigation bar">Click Here</a></div>
 
// my src to my external JS file at the bottom of my page
<script type="text/javascript" src="/scripts/global.js"></script>
 
// function inside JS file
function toggleNavigationBar(height) 
{	
	if(navigationOpened == false) 
	{
		resizeDivHeight.custom(0,height);
		navigationOpened = true;
	} 
	else 
	{
		resizeDivHeight.custom(height,0);
		navigationOpened = false;
	}
}

Open in new window