Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1010
  • 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
FSIFM
Asked:
FSIFM
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
 
b0lsc0ttCommented:
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
 
FSIFMAuthor 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
 
b0lsc0ttCommented:
Your welcome!  I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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