Jump to content

[PHP/JS] Passing var's in from PHP to JS?


Full-Demon

Recommended Posts

Hi,

 

I have a small problem, I have a variable in PHP which I would like to pass to JS. However, a form won't work, because JS is initialised between the <head> tag's. Echo a variable with PHP in JS doesn't work, because the JS file in external. And passing var's via the URL won't work because the php file is called with a virtual URL.

 

I hoped something like this would work:

 

<script type="text/javascript" src="javascript.js?var=thisismyvar"></script>

 

But when I call:

 

document.location.href

 

It returns the URL which is displayed in the address bar for the php file! So not for the JS file. However, I saw some scripts that worked with the above example (it works in my website) but I cant get it to work, very weird written:

 

var Scriptaculous = {
  Version: '1.5.1',
  require: function(libraryName) {
    // inserting via DOM fails in Safari 2.0, so brute force approach
    document.write('<script type="text/javascript" src="'+libraryName+'"></script>');
  },
  load: function() {
    if((typeof Prototype=='undefined') ||
      parseFloat(Prototype.Version.split(".")[0] + "." +
                 Prototype.Version.split(".")[1]) < 1.4)
      throw("script.aculo.us requires the Prototype JavaScript framework >= 1.4.0");
    
    $A(document.getElementsByTagName("script")).findAll( function(s) {
      return (s.src && s.src.match(/scriptaculous\.js(\?.*)?$/))
    }).each( function(s) {
      var path = s.src.replace(/scriptaculous\.js(\?.*)?$/,'');
      var includes = s.src.match(/\?.*load=([a-z,]*)/);
      (includes ? includes[1] : 'builder,effects,dragdrop,controls,slider').split(',').each(
       function(include) { Scriptaculous.require(path+include+'.js') });
    });
  }
}

Scriptaculous.load();

 

This script gets called by:

 

<script type="text/javascript" src="scriptaculous.js?load=effects"></script>

 

How can I fix this?

 

Thanks,

 

FD

Link to comment
Share on other sites

It doesnt work because the JS is external. And pasting isnt really an option, as I include the headers different on each page...and its a very long javascript file.

 

There must be a way like the scriptaculous script does it, because that one works...but Im not a master in JS (I hate the language, because its so messy), so I cant find out what part actually retrieves the value...

 

Thanks for the reply though

 

FD

Link to comment
Share on other sites

you can use inline js and external js together:

<script>
var phpVar="<?php echo($phpVar);?>";
</script>
<script src="external file url"></script>

the js variable recieved from php, phpVar, will be available to the js code in the external js file.  As well, leaving your external file with just a .js extension and no '?' will cause the external file to be cached better and load faster.

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.