Quick HTML/JS Scripting Question

Soldato
Joined
25 Oct 2007
Posts
6,911
Location
Los Angeles
Hi Guys - I’ve been asked to implement some very simple logic from a static page. Essentially upon clicking a link, I would need to send a value, interpret the value and then redirect the user to a help page. The only catch is that I will not be able to make use of any server side scripting technologies. So the logic would be something like this:

1) User clicks on link which looks something like this: http://www.url.com?id=1

2) JavaScript interprets URL and redirects to another page based on a switch statement / csv ID to URL mappings

Now i have a feeling that without a server side scripting technology such as php or asp I will be unable to pass the URL? I assume however that I should be able to call a remote JS file that will facilitate the logic?

Can anyone advise the cleanest and easiest way to do this without a server side scripting technology?
 
Thanks sist. I have the following code within a file named cal.js within my wwwroot folder:

Code:
<script language="JavaScript1.2"> //change the scrollers width (in pixels) 
ar scripts = document.getElementsByTagName('script');
var myScript = scripts[ scripts.length - 1 ];

var queryString = myScript.src.replace(/^[^\?]+\??/,'');

var params = parseQuery( queryString );

document.write('Ski conditions in ' + params['town'] +
               ', ' + params['state'].toUpperCase() + '...');

function parseQuery ( query ) {
   var Params = new Object ();
   if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}
</script>
I then call it: http://localhost:88/cal.js?state=nm;town=Taos

The page then displays the code. I'm running windows 7 and have started my default website in IIS. Can someone remind me what I have forgotten?

EDIT: Feel like a complete newb not being able to sort this lol.
 
Last edited:
change:
var queryString = myScript.src.replace(/^[^\?]+\??/,'');

to:
var queryString = window.location.href.replace(/^[^\?]+\??/,'');

and it works for me in a htm page.
 
Thanks sist, what i'm trying to do is the following:

Code:
<html>
<body>

<script type="text/javascript">
ar scripts = document.getElementsByTagName('script');
var myScript = scripts[ scripts.length - 1 ];

var queryString = window.location.href.replace(/^[^\?]+\??/,'');

var params = parseQuery( queryString );

document.write('Ski conditions in ' + params['town'] +
               ', ' + params['state'].toUpperCase() + '...');

function parseQuery ( query ) {
   var Params = new Object ();
   if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}
</script>

</body>
</html>

When I try the following URL: http://localhost/home.htm?state=nm;town=Taos

My browser is blank.
 
Last edited:
Back
Top Bottom