Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-29-2013, 01:34 PM   PM User | #1
hno2005
Regular Coder

 
Join Date: Oct 2009
Posts: 102
Thanks: 0
Thanked 0 Times in 0 Posts
hno2005 can only hope to improve
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
hno2005 is offline   Reply With Quote
Old 01-29-2013, 01:46 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 01-29-2013, 02:16 PM   PM User | #3
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
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.

Last edited by mrhoo; 01-29-2013 at 07:46 PM.. Reason: spelling
mrhoo is offline   Reply With Quote
Old 01-29-2013, 03:17 PM   PM User | #4
hno2005
Regular Coder

 
Join Date: Oct 2009
Posts: 102
Thanks: 0
Thanked 0 Times in 0 Posts
hno2005 can only hope to improve
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
hno2005 is offline   Reply With Quote
Old 01-29-2013, 06:42 PM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 01-29-2013, 08:15 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 01-30-2013, 04:34 AM   PM User | #7
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%

Last edited by rnd me; 01-30-2013 at 04:36 AM..
rnd me is offline   Reply With Quote
Old 01-30-2013, 07:02 AM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:03 PM.


Advertisement
Log in to turn off these ads.