0

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);
2
  • 1
    Could you provide an example? Since PHP runs on the server and jQuery runs in the browser, what do you mean "using php within jquery"? Commented Jun 4, 2009 at 14:22
  • Have updated with an example above Commented Jun 4, 2009 at 14:29

6 Answers 6

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

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!! :)
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.
1

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.

Comments

1

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.

Comments

1

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 :)

1 Comment

this was just a snippet, the tags are elsewhere!
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.