qbox Posted July 12, 2008 Share Posted July 12, 2008 Hi In one site I see that they use link like this http://www.example.com/js/script.js?ident=9iUiaa How they use it this? How to put ident to use in js file? How this stuff work? Thank you Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 13, 2008 Share Posted July 13, 2008 A javascript file can't directly use that variable like PHP can. In this case, they are doing one of two things. 1) The .js file is actually a server side script. This means that even though it's a .js file, they have configured their webserver to treat the file as a server side script (like PHP). Their server side script can then read that GET variable, and return a javascript file. 2) Less likely, the javascript is parsing that value from the HTML in the page. After the page loads, you can grab the SCRIPT elements with document.getElementByTagName('SCRIPT'), search for the one you want, then parse the value off the src attribute Quote Link to comment Share on other sites More sharing options...
qbox Posted July 13, 2008 Author Share Posted July 13, 2008 Thanks But can I make something similar with php? Like to take some information from the user and to send him a js code generated only for him? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 13, 2008 Share Posted July 13, 2008 yes...easiest way to do this, it just make a PHP script and use the header() function [code=script.php?ident=9iUiaa]<?php //It is good to send the content type, but i don't think it's needed header('Content-type: text/javascript'); print "alert('ident is set to: {$_GET['ident']}');"; ?> and it put it on a page: <html> <head> <script type="text/javascript" src="script.php?ident=9iUiaa"></script> </head> <body> ... </body> </html> ...the file doesn't have to be a .js file. but, once you get the above working, you can use Apache's mod_rewrite to 'mask' it as a .js file [/code] Quote Link to comment Share on other sites More sharing options...
qbox Posted July 13, 2008 Author Share Posted July 13, 2008 Thank you. That really help me. Thank you again. 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.