Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,010
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by hno2005 View Post
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    <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.
    Last edited by mrhoo; 01-29-2013 at 07:46 PM. Reason: spelling

  • #4
    Regular Coder
    Join Date
    Oct 2009
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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">
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,506
    Thanks
    77
    Thanked 4,378 Times in 4,343 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,343
    Thanks
    11
    Thanked 589 Times in 570 Posts
    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.
    Last edited by rnd me; 01-30-2013 at 04:36 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,506
    Thanks
    77
    Thanked 4,378 Times in 4,343 Posts
    Interesting! There are still things I'd prefer doing on the server, but I can see the utility of this in many situations.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •