Full-Demon Posted July 8, 2007 Share Posted July 8, 2007 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 Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted July 8, 2007 Share Posted July 8, 2007 Echo a variable with PHP in JS doesn't work that does work. this works fine with me <script> var phpVar="<?php echo($phpVar);?>"; </script> why not paste your js in your head? otherwise you could use ajax to get the php var Quote Link to comment Share on other sites More sharing options...
Full-Demon Posted July 8, 2007 Author Share Posted July 8, 2007 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 Quote Link to comment Share on other sites More sharing options...
mainewoods Posted July 9, 2007 Share Posted July 9, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.