![]() |
How to post some data to a javascipt file
Hi every one
I need to send an id to a JavaScript file . this is my code : <script src="test.js" language="javascript" ></script> I want to post data , something like this : <script src="test.js?id=5" language="javascript" ></script> is it possible ? How can I send an id to a javascript file ? Thanks |
Quote:
Be aware that script language="javascript" is long obsolete. St.John the blacksmith dumped water on his head. - Pupil's answer to Catholic Elementary School test. |
Quote:
and though the fetched file has no idea what you are doing, the server and the client can use it. |
I'm using it for an gallery website . it shows top 10 images .everyone can get this code and use it in his blog or website and then it shows the latest images in his blog.
It has different parameters that user can set , like bg color. This is an example : <script type="text/JavaScript" src="http://www.aparat.com/video/video/scr/type/all/sort/latest/cnt/10/bg/FFFFFF/height/400/id/aparat13041527855789432"></script> how does it send these datas ? How can get them ? Thanks |
You could use a server side language to read the id and make the substitutions into the JavaScript before sending it to the browser. For example if the script were to use PHP to make the changes then the script tag would be:
Code:
<script type="text/javascript" src="test.php?id=5"> |
And you *COULD* use
Code:
<script src="test.js?id=5" language="javascript" ></script>That is, the server would see "test.js?id=5" and know that, for this specific file request, it should rewrite that into "zamboni/frambot.php?zam=117&requested=5" We do something like that with some ".css" files on one server I work on. It's easy to do that with Linux or Windows servers. |
you don't need any server code to catch URL params on a script url, passing that data to the script within. that's actually pretty simple.
throw this in the external script file: Code:
function parseQS(str) {if used on "/scripts/test.js?a=1&b=2&c=hello%20world", you should get this object: Code:
{ |
Interesting! There are still things I'd prefer doing on the server, but I can see the utility of this in many situations.
|
| All times are GMT +1. The time now is 07:19 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.