Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1013
  • Last Modified:

How to retrieve paramaters from a .js url

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
James Glen
Asked:
James Glen
2 Solutions
 
netsmithcentralCommented:
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
 
nizsmoDeveloperCommented:
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
 
b0lsc0ttIT ManagerCommented:
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
 
James GlenIT EngineerAuthor Commented:
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
 
b0lsc0ttIT ManagerCommented:
Your welcome!  I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now