• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

simple question: conditional based on querystring parameter

I need to take this code

and make it something like this (please excuse vb.net coding style, but needs to be transposed to javascript)

if request.querysring("v") is nothing then
     document.write('<embed runat="server" id="movQuicktime" src="http://www.mysite.com/some.mov"')
     document.write('width="320" height="255" autoplay="true" loop="true" controller="true" playeveryframe="false"')
     document.write('pluginspage="http://quicktime.apple.com"></embed>')
else if request.querystring("v") = 2 then
     document.write('<embed runat="server" id="movQuicktime" src="http://www.mysite.com/some2.mov")
     document.write('width="320" height="255" autoplay="true" loop="true" controller="true" playeveryframe="false"')
     document.write('pluginspage="http://quicktime.apple.com"></embed>')
end if
0
rodmjay
Asked:
rodmjay
  • 7
  • 4
1 Solution
 
Dennis MaederCommented:
request.querystring is a server side response to a get request.
javascript is clientside, so there is no direct translation.

D
0
 
rodmjayAuthor Commented:
i dont need an exact translation, just something that does the trick.
0
 
Dennis MaederCommented:
How about a function that you supply with an argument (v) ?
D
0
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.

 
rodmjayAuthor Commented:
even if its something like this it will work

if querystring.contains("v=2") then ...

i just dont know how to program in javascript that well, so i need some help
0
 
Dennis MaederCommented:
Here it is in a little test harness:
remove the alert lines and remove the "if (arg == 2)" FROM else if (arg == 2){
if all you want to do is differentiate between an empty response and something else
D

<script>
function dothis(arg) {
 
if (!arg  || arg == "" ){
     alert ("|"+arg+"|");
     document.write('<embed runat="server" id="movQuicktime" src="http://www.mysite.com/some.mov"');
     document.write('width="320" height="255" autoplay="true" loop="true" controller="true" playeveryframe="false"');
     document.write('pluginspage="http://quicktime.apple.com"></embed>');
}
else if (arg == 2){
     alert ("|"+arg+"|");
     document.write('<embed runat="server" id="movQuicktime" src="http://www.mysite.com/some2.mov" ');
     document.write('width="320" height="255" autoplay="true" loop="true" controller="true" playeveryframe="false"');
     document.write('pluginspage="http://quicktime.apple.com"></embed>');
}
}
dothis ("2");
</script>
0
 
Dennis MaederCommented:
This might be invoked from html something like this
<input name="v" type="text" onchange="dothis(this.value);">
D
0
 
Dennis MaederCommented:
if you are passing a string and testing it for the presence of "v=2"
you might use
else if (arg.indexOf("v=2") >=0 ){
 ...
}

OR

var str = "v=2";
...
if (...){
 ...
}
else if (arg.indexOf(str) >=0 ){ //returns position of str in arg
 ...
}

with the appropriate bits for the ...

D
0
 
rodmjayAuthor Commented:
i dont need a function, just the if statement.  This code is being called from a page like this

<script src="Presentations.js"></script>

and the code looks like this

   document.write('<embed runat="server" id="movQuicktime" src="http://www.mysite.com/some.mov"')
     document.write('width="320" height="255" autoplay="true" loop="true" controller="true" playeveryframe="false"')
     document.write('pluginspage="http://quicktime.apple.com"></embed>')

so all i need is an if statement i think.  I dont want to make any changes to the page, just to the Presentations.js file.
0
 
Dennis MaederCommented:
You have to have a swtching condition which is visible to the presentations.js include on the client side.
There is no need to embed the conditions in a function as long as you can provide a js variable (here arg) which can be used. This can be derived from a form something.value, but it cannot be the result of a form submission on the server.
If you want further advice, please divulge the code for the form thats calling the javascript.
D
0
 
rodmjayAuthor Commented:
i did divuldge the code it is just the script.  Once the tag gets parsed by the browser, it calls the javascript.  I just need a conditional code in the javascript that looks at the url.  Is this some impossible feat?
0
 
BasilisciCommented:
Actually it is quite easy to do this in javascript. You get the query string  like this:

var querystring = "" + document.location.search;

This would return something like "?foo=bar&v=1&k=x", depending on your application.

You need to parse it somehow to check the v variable. Simplest is just to check for the existence of it like this:

var querystring = "" + document.location.search;
if (querystring.indexOf("v=") > -1) {
  // some code
} else {
  // some code
}
0
 
Dennis MaederCommented:
If what  you want is the value of v in the url, this should work:
var vval = "";
var q = document.location.search;
q = q.substring(1); // strip off the leading '?'
var namevals= q.split("&");
for (i = 0; i < namevals.length; i++){
  var nv = namevals[i].split("=");
  var name = nv[0];
  var value = nv[1];
  if (name == "v"){
    vval = value;
  }
}
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.

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