beliy333 beliy333 - 1 year ago 100
PHP Question

Is it okay to save my javascript in a .php format for a wordpress site?

Instead of putting my javascript into a .js file, I would like to use a .php file so that I can use some php code (for example getting my wordpress blog url, or "if this page" statements).

Are there any down sides in terms of performance if I do this?

Answer Source

You can have a .php page and have your js functions within it with the proper <script></script> tags, as you said, and there shouldn't be anything wrong with it.

Though, if you have lots of js functions that require its own page, then you can pass the values that come from the server-side to the function in the client side. For example.

in .php page

var blogUrl = "<?php echo get_bloginfo('url'); ?>";

myFunction (blogUrl);


and in .js page

function myFunction (blogUrl)
   //do something 

This way, you can have a proper pure js file, while not making your php files too messy with JavaScript either.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download