Jump to content

[SOLVED] Convert Javascript variables


ghgarcia

Recommended Posts

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]

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

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.

 

 

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

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

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.

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"); 

 

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>

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.