CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   How to post some data to a javascipt file (http://www.codingforums.com/showthread.php?t=286628)

hno2005 01-29-2013 01:34 PM

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

Philip M 01-29-2013 01:46 PM

Quote:

Originally Posted by hno2005 (Post 1309434)
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

Not possible. (Why do you want to do that??) JavaScript running in the browser does not have any commands for reading or writing files. In any case for security reasons JavaScript cannot access anything beyond the domain of the current page. This is known as the "same origin policy" and prevents a document or script loaded from one origin from getting or setting properties of a document from a different origin.


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.

mrhoo 01-29-2013 02:16 PM

Quote:

<script src="test.js?id=5"
Adding a search string to any downloaded resource is a GET, not a POST-

and though the fetched file has no idea what you are doing,

the server and the client can use it.

hno2005 01-29-2013 03:17 PM

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

felgall 01-29-2013 06:42 PM

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">

Old Pedant 01-29-2013 08:15 PM

And you *COULD* use
Code:

<script src="test.js?id=5" language="javascript" ></script>
if you used URL-rewriting on the server.

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.

rnd me 01-30-2013 04:34 AM

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) {
        var ob = {}, float = "",
                key = "",
                dc = decodeURIComponent;

        for (var i = 0, mx = str.length; i < mx; i++) {
                var it = str[i];
                if (it === "=") {
                        key = float;
                        float = "";
                        continue;
                }
                if (!it.search(/^[?&]/)) {
                        if (it === "&" && str.slice(i + 1, i + 5) === "amp;") {
                                i = (i + 4);
                                float += "&";
                                continue;
                        }
                        if (key) {
                                ob[key] = dc(float);
                        }
                        key = "";
                        float = "";
                        continue;
                }
                float += it;
        }
        ob[key] = dc(float);
        return ob;
}


var scripts=document.getElementsByTagName("script");
var options=parseQS(scripts[scripts.length-1].src);

alert(

  JSON.stringify( options , null , '\t' )

)

so long as you don't use a defer or async attrib on the externally-pointing script tag, this will work just fine.


if used on "/scripts/test.js?a=1&b=2&c=hello%20world", you should get this object:
Code:

{
        "a": "1",
        "b": "2",
        "c": "hello world"
}

this can allow the people using the embed to adjust the url params to set options like color, ID, etc. you could use a URL-builder form interface to allow such config options to be set using a GUI instead of editing params, much like vimeo or youtube does. this is all you need if your external script can handle all the options.

Old Pedant 01-30-2013 07:02 AM

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.