Solved

simple question: conditional based on querystring parameter

Posted on 2006-12-01
12
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 
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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to calculate height of a text using jquery 3 42
javascript form submition 2 21
Html split(text) Part2 6 21
REST call Failing 1 10
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

756 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