Solved

How to retrieve paramaters from a .js url

Posted on 2007-11-29
5
997 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
5 Comments
 
LVL 12

Accepted Solution

by:
netsmithcentral earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Your welcome!  I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now