#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    16
    Rep Power
    0

    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
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    all scripts run in the same context, so you could simply define a global variable in a script element to be used it in your external script:
    Code:
    <script type="text/javascript">
    	id = 5;		// initialize variable for test.js
    </script>
    <script src="test.js" type="text/javascript"></script>
    Note, however, that you should generally avoid globals variables and hidden dependencies like that. If possible, wrap your code in an object or a function and pass the value as an argument. This way you can actually see where it comes from.

    Also note that the language attribute is long obsolete. Use
    Code:
    type="text/javascript"
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    16
    Rep Power
    0
    there is just a small problem , if the user puts 2 scripts like this in his website ,just one of them will work .
    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
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by hno2005
    there is just a small problem , if the user puts 2 scripts like this in his website ,just one of them will work .
    Not sure what you mean by that. The global variables are available to any script, no matter how many there are.

    Including an external script like in your example has little to do with passing variables to JavaScript. What it does is this: It sends a request to some server script (like a PHP file), which will use the parameters to generate(!) JavaScript code and send it back to the requesting page. So the data is not being sent to a JavaScript file. It's passed to a server script, which dynamically creates JavaScript code.

    JSONP works like this.

    So the question is: Do you want to generate JavaScript code depending on certain parameters? In that case, you need to set up a PHP script or something. Or do you have a given JavaScript file, which you want to send data to? In that case, do what I suggested above.
    Last edited by Jacques1; January 29th, 2013 at 10:57 AM.

IMN logo majestic logo threadwatch logo seochat tools logo