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

x
?
Solved

simple question: conditional based on querystring parameter

Posted on 2006-12-01
12
Medium Priority
?
324 Views
Last Modified: 2012-06-27
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
Comment
Question by:rodmjay
  • 7
  • 4
12 Comments
 
LVL 10

Expert Comment

by:Dennis Maeder
ID: 18056428
request.querystring is a server side response to a get request.
javascript is clientside, so there is no direct translation.

D
0
 
LVL 8

Author Comment

by:rodmjay
ID: 18056477
i dont need an exact translation, just something that does the trick.
0
 
LVL 10

Expert Comment

by:Dennis Maeder
ID: 18056777
How about a function that you supply with an argument (v) ?
D
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Author Comment

by:rodmjay
ID: 18056796
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
 
LVL 10

Expert Comment

by:Dennis Maeder
ID: 18056835
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
 
LVL 10

Expert Comment

by:Dennis Maeder
ID: 18056881
This might be invoked from html something like this
<input name="v" type="text" onchange="dothis(this.value);">
D
0
 
LVL 10

Expert Comment

by:Dennis Maeder
ID: 18057355
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
 
LVL 8

Author Comment

by:rodmjay
ID: 18060829
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
 
LVL 10

Expert Comment

by:Dennis Maeder
ID: 18060950
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
 
LVL 8

Author Comment

by:rodmjay
ID: 18061718
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
 
LVL 6

Accepted Solution

by:
Basilisci earned 2000 total points
ID: 18063647
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
 
LVL 10

Expert Comment

by:Dennis Maeder
ID: 18065982
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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

971 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