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]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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>

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.