Solved

simple question: conditional based on querystring parameter

Posted on 2006-12-01
12
305 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
Comment Utility
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
Comment Utility
i dont need an exact translation, just something that does the trick.
0
 
LVL 10

Expert Comment

by:dennis_maeder
Comment Utility
How about a function that you supply with an argument (v) ?
D
0
 
LVL 8

Author Comment

by:rodmjay
Comment Utility
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
Comment Utility
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
Comment Utility
This might be invoked from html something like this
<input name="v" type="text" onchange="dothis(this.value);">
D
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 10

Expert Comment

by:dennis_maeder
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now