JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 29th, 2013, 07:35 AM
hno2005 hno2005 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 11 hno2005 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 45 m 51 sec
Reputation 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

Reply With Quote
  #2  
Old January 29th, 2013, 08:38 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,847 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 10 h 36 m 10 sec
Reputation Power: 813
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"

Reply With Quote
  #3  
Old January 29th, 2013, 09:18 AM
hno2005 hno2005 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 11 hno2005 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 45 m 51 sec
Reputation 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

Reply With Quote
  #4  
Old January 29th, 2013, 09:54 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,847 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 10 h 36 m 10 sec
Reputation Power: 813
Quote:
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 09:57 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > How to post some data to a javascipt file

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap