javascript in php

MacPC

New Member
Hi,

I am stuck. I am trying to put some lines of javascript code in a php file

The excerpt of the code looks like this:

<?php
....
echo 'var count=0;';
echo 'var ns6=document.getElementById&&!document.all';
echo 'var ie4=document.all&&navigator.userAgent.indexOf("Opera")==-1';

echo 'if (ie4||ns6){
document.write('<form name="headline"><input type="button" name="display_msg" style="font-size:12px;color:#1c6f41;font-weight:bold;background:transparent; width:'+barwidth+'; height:12; border-width:0; border-color:#1c6f41;" onmouseover="this.style.background=mouseover_color" onmouseout="this.style.background=mouseout_color"></form>');
}';

echo 'function init_news_bar(){
document.news_bar.display_msg.value=msgs[count];
}';
...
?>


I am having trouble with the highlighted section, where in the javascript already has a mixture of " and ' already, I can't use echo 'xyz'; how do I get around that?

Any help will be greatly appreciated.

MacPC
 

puritys

New Member
echo "<script>";
echo 'var count=0;var barwidth=225;';
echo 'var ns6=document.getElementById&&!document.all;';
echo 'var ie4=document.all&&navigator.userAgent.indexOf("Ope ra")==-1;';

echo 'if (ie4||ns6){
document.write(\'<form name="headline"><input type="button" name="display_msg" style="font-size:12px;color:#1c6f41;font-weight:bold;background:transparent; width:"+barwidth+"; height:12; border-width:0; border-color:#1c6f41;" onmouseover="this.style.background=mouseover_color " onmouseout="this.style.background=mouseout_color" value="it is the value"></form>\');}';
echo '</script>';

try this! just add \ before '
:)
 

MacPC

New Member
Hi thanks for the reply.

I havn't try your suggestion yet. But what I did was to step out of php like this:

<?php
...

?>

<script type="text/javascript">
var....

/* Do the js thing */
</script>
<!--Here I come back to php-->
<?php

.....
?>

This seems to work fine, but is it the correct way to do it?

M
 

Imagn

New Member
Though it's minimal in this instance inline HTML/JavaScript is faster than using PHP to generate it. There's never really a "correct" way :)
 
Top