I am using the shortcode execute plugin in wordpress.
This simply allows me to write shortcode like this [email_spamproof]
But I am trying to echo a script.
It is easy. Save your javascript code in a new one file and then use this to load it:
include('myjavascript.php');
Then you will use the include
option as a echo
because the web will understand your code as HTML and you execute it when you want with php
(like echo
).
You could write " in your javasript code, and then only use single quotes(') in your php echo.
A more suitable method is to use escape characters in your javascript code "\'" (without the double quotes)
The \
character is the escape character. Prepend one to each internal use of the character used to delimit the string.
Alternatively, use a HEREDOC.
You can either escape them with a back slash: echo 'It\'s cold';
or use heredoc
:
echo <<<END
lots of text here
END;
The right code:
<?php
echo "<script type='text/javascript'>
<!--
// spam protected email
emailE=('enquiries@' + 'example.co.uk')
document.write('<a title='E-mail Example' href='mailto:' + emailE + ''>' + emailE + '</a>')
//-->
</script>
<noscript>
<span class='spam-protected'>Email address protected by JavaScript. Please enable JavaScript.</span>
</noscript>";
?>
You need to escape them and then concatenate the variables :
(Also I dont know if you realize, but you are echoing a commented out section of javascript)
echo '<script type="text/javascript">
<!-- // spam protected email
emailE=("enquiries@" + "example.co.uk")
document.write(\'<a title="E-mail Example" href="mailto:'.emailE.'">'.emailE.'</a>\')
//-->
</script>
<noscript>
<span class="spam-protected">Email address protected by JavaScript. Please enable JavaScript.</span>
</noscript>';