views:

462

answers:

6

Is this bad practice to use php statements such as 'if' within a jquery function?

should I really just be writing loads more functions instead of limiting a few using php? it seems to work fine but was wondering if this is a major faux pax?

example:

this is a code snippet, basically if the php variable $url is equal to 'projects' then the code will come back with extra lines, otherwise these will be hidden from other pages. Does this make sense?

timer = setTimeout(function() {
       $('li#contact').removeClass('cur');
       $('li#$url').addClass('cur');
       "; if($url=='projects') { echo"
       $('ul.projects').fadeIn('slow');
       $('ul.image_display').css('display', 'block');"; }
       echo"
        }, 8625);
A: 

I don't think it's bad practice. I use ASP.NET literals to write out dynamic jQuery based on data that comes back from the database. I don't do it a lot but there are times when it's needed.

RedWolves
+1  A: 

it depends. if you're just writing out some user data for javascript to consume (e.g. a user id or name), than no. But if you're using PHP a lot for control structures, there's probably a better way to do it.

One of the big downsides of your approach (other than having code that's a little ugly) would be preventing the js to be cached nicely. By having the js dynamically created and not in a separate, compressed js file, you loose the ability to cache it and hurt your bandwidth.

nategood
+1  A: 

shouldn't you put your php code between php tags then? Otherwise, it will just show up in the source code instead of being processed at the server.

timer = setTimeout(function() {
                    $('li#contact').removeClass('cur');
                    $('li#$url').addClass('cur');
                    "; <?php if($url=='projects') { echo"
                    $('ul.projects').fadeIn('slow');
                    $('ul.image_display').css('display', 'block');"; }
                    echo"
                            } ?>, 8625);

off topic: some better formatting would be nice too :)

Fortega
this was just a snippet, the tags are elsewhere!
DanC
+1  A: 

Not bad practice as sometimes a PHP variable's value will change what you want the code to do.

You will however have to use to open and close your PHP code.

Andrew G. Johnson
so basically the php may effect caching but not the code performance and so not really bad practice??means i don't have to go rewrite it all at least!! :)
DanC
Basically, also you don't want to cache something that changes. I suppose in theory you could do a PHP if else and have both versions of the javascript in separate .js files -- but truth be told for a site that probably isn't all that huge and a code snippet that is so small I wouldn't.
Andrew G. Johnson
+3  A: 

This is a bad practice in terms of code readability and maintainability, in my opinion. If only because it's two languages embedded within each other, which means you can't understand it without parsing two languages, and for many people that causes a mental 'page fault' as they context switch between parsing JS and parsing PHP.

It is easy enough to have the javascript branch based on what page elements are present, and this is a preferred solution in my opinion. It's better than conditionally providing certain javascript (although I think providing different javascript may begin to make sense when you have an overwhelmingly large amount of js) and it's also better than branching based on URL... what happens when you go and refactor that?

If you really must branch which JS you supply based on PHP, I'd want to see them at least on separate lines... something like:

<?php if($stmt): ?>
$(javascript).code.goes.here
<?php else: ?>
$(alernative.javascript).code.goes.here
<?php endif; ?>

as that would be nicer than having to read and understand two languages in a single line/statement.

Daniel Papasian
+1  A: 

I try to keep all the code separated as much as possible (HTML/CSS/PHP/JS), because I don't like large chunks of JS code within my HTML or PHP. What I usually do with variables is just place them somewhere in the HTML, e.g. <div id="url">value</div> and then retrieve it using jQuery. Depending no the variables, I sometimes have a few of these div's somewhere hidden on the page using display:none.

This way I can still use the same JS code for multiple applications, depending on the retrieved values. I must say though that I have no idea whether this is considered ok, or that this setup/method is (also) not-done.

Alec