Just as a follow up, in PHP when you echo strings quoted in double quotes, that allows for the resolution of variables, globals, etc. - all kinds of things, according to the rules/logic of PHP. Sometimes unexpected results occur. I just tried your code on my wamp server and got:
Parse error: parse error, expecting `T_VARIABLE' or `'$'' in test_script.php on line 7
Depending upon how one counts lines, that's one of these:
Code:
echo " $('.magnify').each(function(){";
echo " if(this.style.zIndex){$(this).click();}";
echo " });";
probably the middle one. The unquoted parenthetical preceded by a $ could be expected to resolve, I guess - PHP isn't my strong suit. But I'm sure its something at least vaguely like that.
Only by using single quotes are you assured of a literal echo. Also in javascript line breaks can be important. I don't think they are here. But it never hurts to play it safe. So you could have done:
PHP Code:
echo '<script type="text/javascript">' . "\n";
echo 'jQuery(function($){' . "\n";
echo ' $(document).click(function(e){' . "\n";
echo ' if(e.target.className === "magnify"){return;}' . "\n";
echo ' $(".magnify").each(function(){' . "\n";
echo ' if(this.style.zIndex){$(this).click();}' . "\n";
echo ' });' . "\n";
echo ' });' . "\n";
echo '});' . "\n";
echo '</script>' . "\n";
or:
PHP Code:
echo '<script type="text/javascript">
jQuery(function($){
$(document).click(function(e){
if(e.target.className === \'magnify\'){return;}
$(\'.magnify\').each(function(){
if(this.style.zIndex){$(this).click();}
});
});
});
</script>';
or even:
Code:
?>
<script type="text/javascript">
jQuery(function($){
$(document).click(function(e){
if(e.target.className === 'magnify'){return;}
$('.magnify').each(function(){
if(this.style.zIndex){$(this).click();}
});
});
});
</script>
<?php
What you did however is fine. And there are probably other options for dealing with this sort of situation.
Bookmarks