?
Solved

How to retrieve paramaters from a .js url

Posted on 2007-11-29
5
Medium Priority
?
1,012 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 1000 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 1000 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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…
Suggested Courses

601 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