Jump to content

Help on parsing external javascript


GuitarGod

Recommended Posts

Hi guys,

 

Well, I have a basic templating system in which a HTML file is simply included, and that HTML file contains some PHP.

 

mypage.htm:

<td>Your name is: <?= $name; ?></td>

mypage.php:

<?php

$name = 'John';
include( 'mypage.htm' );

?>

 

One of my HTML files links to a javascript file:

 

<script src="myscripts.js"></script>

 

The trouble is, any PHP code within myscripts.js is not parsed. Is there a way round this? It works when I simply paste the functions from myscripts.js into the HTML template, but I really want to avoid this.

 

Any help is, as always, greatly appreciated. ;D

Link to comment
Share on other sites

myscripts.js.php

 

To elaborate. You need to set it as a .php extension and I believe you should also send the header content-type as text/javascript since that is the type of file you are dishing out. It is "not" required to send the content-type. But I just like to :)

Link to comment
Share on other sites

What page is myscripts.js? Can you post what you have for myscripts.js? Or did you mean to say mypage.php?

 

You can use a PHP extension for scripts. But the catch is that your PHP file has to output JavaScript code! For example:

 

mypage.php

$e = 'Welcome John!';
echo 'document.write("' . $e . '");';

 

index.html

<script type="text/javascript" src="path/to/mypage.php"></script>

 

That should work.

Link to comment
Share on other sites

Hi,

 

What I mean is, throughout my site, there are language variables (as the site is multi-lingual), for example, in index.htm I could have <?= $lang->Hello; ?>, and it works fine. But the language variables in myscripts.js don't work; I could have alert('<?= $lang->Hello; ?>') but it doesn't work. So I need a way of parsing all the language variables in myscripts.js. I tried changing the extension to php, but it didn't work.

 

Any other ideas?

Link to comment
Share on other sites

External javascript files are requested/fetched by the browser. If you have php code in them, that php code would need to have access to the php variables you are trying to use at the time the file is requested/fetched. Best I can tell is that you are setting these php variables in the php code on your main page.

 

If you expect php variables that are set in your maiin php file to be available in javascript on that page, you would need to instead use a php include to get that external javascript into the main php page and make it inline javascript.

 

If you must use an external javascript file, you will need to either put the variables into session variables or separate out the variables into their own include file. Then the external javascript file would either resume the session or include the file with them in order to gain access to the values.

Link to comment
Share on other sites

In your JavaScript file (which should have the extension .php and *NOT* .js). Unless you told your server to treat .js files as .php, PHP won't run on it.

 

In your now .php file, include the class lang or whatever it's called and use it. It should work now. And you can use it as an external JS file as I've mentioned in a previous post in this topic.

Link to comment
Share on other sites

Index.php

 

<?php

// Lots of irrelevant code here
//

include( 'index.htm' );

?>

 

index.htm

 

<html>
<title> ... etc

<script language="javascript" type="text/javascript" src="scripts.js.php"></script>

 

scripts.js.php

 

<? include( 'language.php' ); ?>

function somefunction()
{
    alert( '<?= $lang->Hello; ?>' );
}

 

language.php

 

<?php

$lang = new stdClass();
$lang->Hello = 'Hi';

?>

 

Hope that helps

Link to comment
Share on other sites

Your problem is likely because of the lazy-way short open tags that are being used in scripts.js.php

 

Change all <? to <?php and <?= to <?php echo

 

Edit: In fact, making that change causes the alert box to work for the code you posted. More wasted time caused by short open tags. Don't use short open tags, ever. Sigh...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.