ghgarcia Posted February 11, 2007 Share Posted February 11, 2007 I have a javascript to display a ticker on my site. I'd like to be able to modify some variables within the javascript using php. The following is the way the variables are listed within the javascript: TICKER_CONTENT = document.getElementById("TICKER").innerHTML; TICKER_RIGHTTOLEFT = false; TICKER_SPEED = 2; TICKER_STYLE = "font-family:Arial; font-size:12px; color:#000000"; TICKER_PAUSED = false; I need a function to read the variables/parameters before the rest of the script is executed. The following is what my current code looks like. <DIV ID="TICKER" STYLE="display:none; border-top:1px solid #CCCCCC; border-bottom:1px solid #CCCCCC; overflow:hidden; width:100%" onmouseover="TICKER_PAUSED=true" onmouseout="TICKER_PAUSED=false" > <? $football->getTicker1(); ?> </DIV> <script type="text/javascript" src="scripts/webticker_lib.js" language="javascript"></script> You can see the script in action at http://lvbash.com/phpfootball20 I've also attached the javascript file. Any help would be greatly appreciated. G [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
fenway Posted February 11, 2007 Share Posted February 11, 2007 Convert how? Quote Link to comment Share on other sites More sharing options...
ghgarcia Posted February 11, 2007 Author Share Posted February 11, 2007 I would like to store the variables in the html to be used by the javascript. This way it would not require modification to the javascript file to change the variables. I currently use a php function to create the display I would just like to be able to mod the variables at the server. Thanks, G Quote Link to comment Share on other sites More sharing options...
fenway Posted February 13, 2007 Share Posted February 13, 2007 Varibles in html? Do you mean hidden inputs? Quote Link to comment Share on other sites More sharing options...
ghgarcia Posted February 13, 2007 Author Share Posted February 13, 2007 Not relly I just need something that can be set by PHP on the server and read by the javascript on the client. Thanks, G Quote Link to comment Share on other sites More sharing options...
fenway Posted February 13, 2007 Share Posted February 13, 2007 OK... well, you can write out a JS object using PHP... Quote Link to comment Share on other sites More sharing options...
mainewoods Posted February 14, 2007 Share Posted February 14, 2007 what you can do is this: TICKER_RIGHTTOLEFT = <?php if ($phpvar) echo 'true' else echo 'false'; ?>; TICKER_SPEED = <?php echo $phptickspeed; ?>; TICKER_STYLE = "<?php echo 'font-family:' . $phpfontfamily . '; font-size:' . $phpfontsize . 'px; color:' . $phpcolor . ';'; ?>"; TICKER_PAUSED = <?php if ($phptickpaused) echo 'true' else echo 'false'; ?>; --the php values printed out above could be retrieved from a db, a flat file, or some other source --watch out for the semicolons! TICKER_STYLE uses css, php, and javascript semicolons all on the same line. Quote Link to comment Share on other sites More sharing options...
ghgarcia Posted February 14, 2007 Author Share Posted February 14, 2007 That is exactly what I want to do however those variables are currently in the javascript .js file. So I need to make them external and need code within the javascript to read them. Thanks for the help. G Quote Link to comment Share on other sites More sharing options...
fenway Posted February 14, 2007 Share Posted February 14, 2007 That is exactly what I want to do however those variables are currently in the javascript .js file. What does this mean? Quote Link to comment Share on other sites More sharing options...
ghgarcia Posted February 14, 2007 Author Share Posted February 14, 2007 It means that the variables used by my javascript are contained within the file. The following is what that looks like: // WebTicker PHPFootball TICKER_CONTENT = document.getElementById("TICKER").innerHTML; TICKER_RIGHTTOLEFT = false; TICKER_SPEED = 2; TICKER_STYLE = "font-family:Arial; font-size:14px; color:#ffffff"; TICKER_PAUSED = false; ticker_start(); function ticker_start() { What I want is to remove the variables from the javascript file and have them be part of the html. This way I could modify the variables with PHP before they are sent to the client. Hope this answere your question. Thanks, G Quote Link to comment Share on other sites More sharing options...
fenway Posted February 14, 2007 Share Posted February 14, 2007 What I want is to remove the variables from the javascript file and have them be part of the html. This way I could modify the variables with PHP before they are sent to the client. This is what doesn't make sense to me -- there are no variable in HTML -- just FORM fields at best. Sounds like what you want is to read in the JS variables in a subsequent PHP block, modify them, and have this "catch" in subsequent JS calls. That's tricky -- and I don't see what it would accomplish. I guess if you knew the names of the variables, you could do this. Quote Link to comment Share on other sites More sharing options...
mainewoods Posted February 14, 2007 Share Posted February 14, 2007 change your filename to 'javascript.php' <script type="text/javascript" src="javascript.php"></script> --php code can then be executed in that javascript file. It's perfectly valid just make sure you only return js code. --use this at the top of the 'javascript.php' file: header("content-type: text/javascript"); Quote Link to comment Share on other sites More sharing options...
ghgarcia Posted February 14, 2007 Author Share Posted February 14, 2007 I've made the suggested changes however now the ticker script doesn't execute on the client. Any ideas? Thanks, G Quote Link to comment Share on other sites More sharing options...
ozfred Posted February 15, 2007 Share Posted February 15, 2007 That is exactly what I want to do however those variables are currently in the javascript .js file. So I need to make them external and need code within the javascript to read them. Wrap the code provided by mainewoods in a script element that is placed before the one that loads the script file. Remove the variables from the script file. <script type="text/javascript"> // mainewoods code here </script> <script type="text/javascript" src="ticker.js"></script> Quote Link to comment Share on other sites More sharing options...
ghgarcia Posted February 15, 2007 Author Share Posted February 15, 2007 That did it the ticker is now working correctly. Thanks to everyone who helped I really appreciate it. G Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.