Solved

How to retrieve paramaters from a .js url

Posted on 2007-11-29
5
1,003 Views
Last Modified: 2008-02-01
HI Experts,

In my page I have:

<SCRIPT LANGUAGE="JavaScript" SRC="../feeder.js?fontFamily=Verdana"></SCRIPT>

which goes off, gets an RSS feed converts to JavaScript and displays on the screen.
I want to allow cusomisation to the style of the feed so want to allow the uses to modify the font, colour, size etc  by passing it through the URL:

eg ../feeder.js?fontFamily=Verdana

Is it possible to read these parameters in Javasscript?

I know how to retireve the querystring parameter in ASP and PHP no problem but not 100% on javascript

I have tried the following from a previous thread but this reads the main URL eg index.html and not the feeder.js?fontFamily=Verdana:

Any help would be much appreciated
function getParam(theKey){
  var qV = location.search.split(RegExp("[?&]"+theKey+"="));
  alert(srcURL)
  if(qV.length>1){
    return unescape((qV[1]+"&").split("&")[0]);
  }
}
 
var clearVal = getParam("fontFamily");

Open in new window

0
Comment
Question by:FSIFM
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 12

Accepted Solution

by:
netsmithcentral earned 250 total points
ID: 20377088
Javascript (.js) files are parsed by the page calling them.  This means they don't see the path used to access them, and hence, no querystring variables.  You can define a variable on the calling page and use it on the script page though.
<!-- PAGE "Caller.html" -->
<html>
<head>
<title>Caller</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
var foo = 'bar';
</script>
<script type="text/javascript" src="script.js"></script>
</head>
</html>
 
// Page "script.js"
window.onload = function() {
	alert( foo ) ;
};

Open in new window

0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20378000
Similar question here:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_22988598.html

Hope this helps. You can simply pass a argument into the js file function by calling the function in your script/html file and passing the value in, eg:

myfunction(myvalue);
0
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 250 total points
ID: 20378012
FSIFM,

I have two suggestions.  What you are doing almost makes it seem like you want the js file to be dynamic.  If that is the case and if you use a server language then this could be an option.  The server lanaguage would see the URL and query string used in the script tag and could use it.  The js file would end with .asp, .php, or some other server page extension but would send a header specifying the content-type as text/javascript.  That last part is a key for this option to work.

Another option would be to see if Javascript could get it using the script tag.  I don't know for sure it would work but it would be done with something like ...

document.getElementsByTagName('script')[0].src

That assumes the script tag is the first script tag (or only) on the page.

Let me know if you have any questions or need more information.

b0lsc0tt
0
 
LVL 4

Author Comment

by:FSIFM
ID: 20380889
unfortunately I can not set a variable into the page eg:

var foo = bar

As the link will be placed into other sites by people wanting to view an RSS feed in their own site and dont think they would want to place other javascript variables into their site. And as for server side links (.ASP, .PHP) some people dont want to place these links as they see them as a security risk but are willing to include a .js file?? Go figure.

I guess I will have to set a standard style sheet and make customisations when needed for the clients.

Thanks for all the help
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20383586
Your welcome!  I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question