Forum Moderators: coopster

Message Too Old, No Replies

calling a function that contains javascript

How?

         

hafnius

5:13 pm on Jun 21, 2003 (gmt 0)

10+ Year Member



Hi There

I have a functionlist that i use to store the repeating page elements - basic stuff.

One of the functions contains javascript and php will not accept this and makes a parse error. How can i contain the javascript in the function so that both php can contain it and so that the result on the page is working javascript.


the function:

function script() {
echo '<a href="foo" id="d" title="" onMouseOver="MM_shl('bug','','show')" onClick="MM_shl('bug','','show')"
onBlur="MM_shl('bug','','hide')" onMouseOut="MM_shl('bug','','hide')">foo</a>';
}

calling the function:

<?php script()?>

kind regards
/Hafnius

jatar_k

7:30 pm on Jun 21, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hm, I don't totally understand. I assume you want to write the js to the output?

you can escape it like so

?>
javascript stuff
<?

or you can echo/print it

Birdman

7:34 pm on Jun 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use all singe quotes or escape(\") the doubles and it should be okay.

jatar_k

7:35 pm on Jun 21, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



ok, more coffee then, thanks Birdman, I seem to have totally missed the echo, sorry hafnius.

I would also use double quotes around the whole string but thats me and since I feel badly

echo "<a href=\"foo\" id=\"d\" title=\"\" onMouseOver=\"MM_shl('bug','','show')\" onClick=\"MM_shl('bug','','show')\" onBlur=\"MM_shl('bug','','hide')\" onMouseOut=\"MM_shl('bug','','hide')\">foo</a>";

hafnius

12:18 am on Jun 22, 2003 (gmt 0)

10+ Year Member



HI Jatar_k and Birdman

thanks - your example, Jatar_k, works like a charm. Its so good that us beginners can get some help too.

Kind regards
/Hafnius

dingman

1:19 am on Jun 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not to impugn the other solutions, but I find that heredoc syntax [php.net] is often just the thing for stuff like that. It's a lot more readable than a whole bunch of \" or \' sequences in the middle of a string, and since it interpolates like double quoted strings it's better for bits where you need to insert varable values than bouncing back and forth between HTML and PHP over and over.

Birdman

9:53 am on Jun 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dingman, that does sound like a cleaner solution. Or, as I just thought, just jump out of PHP since there are no vars in the function.

function script() {
?>
<a href="foo" id="d" title="" onMouseOver="MM_shl('bug','','show')" onClick="MM_shl('bug','','show')"
onBlur="MM_shl('bug','','hide')" onMouseOut="MM_shl('bug','','hide')">foo</a>

<?php
}