oneoleguy Posted December 13, 2007 Share Posted December 13, 2007 Hello,I want to learn how to pass arguments to HTML code. Here is an example of what I want to do:myfile1.html&arg=AdLocation1Within myfile1.html, I want to take the value of the argument and add a fixed value to it like 'from site8' and then pass it on to another HTML file, likemyfile2.html&newarg=the combined argumentCan someone direct me to a document that explains how to do this, or just tell me here on this forum?Thanks,Bill Link to comment Share on other sites More sharing options...
justsomeguy Posted December 13, 2007 Share Posted December 13, 2007 You could do something like that using only Javascript, but if your server supports a language like PHP that will give you a lot more flexibility. With PHP it would be as easy as something like this: <?phpprint "<a href=\"page2.php?arg=" . $_GET['arg'] . "fixed_value" . "\">Page 2</a>";?> Of if you just want to redirect: <?phpheader("Location: page2.php?arg=" . $_GET['arg'] . "fixed_value");?> With Javascript you would need to get the URL of the page, break up the arguments that were passed to it, find the one you're looking for, and update another link somewhere on the page (or redirect). That would be something like this: <script type="text/javascript">var qs = window.location.search;qs = qs.substr(1);var args = qs.split("&");var arg_value = "";for (var i = 0; i < args.length; i++){ pair = args[i].split("="); if (pair[0] == "arg_name") { arg_value = pair[1] + "fixed_value"; break; }}if (arg_value != ""){ // write the argument to an existing link with an ID document.getElementById("link_id").href += "?arg=" + arg_value; // or redirect window.location.href = "page2.html?arg=" + arg_value;}</script> Link to comment Share on other sites More sharing options...
oneoleguy Posted December 15, 2007 Author Share Posted December 15, 2007 Mr. ParanoidI was unable to understand what either the PHP snippets did, so I started trying to see what the Javascript did.I want to pass a variable, such as, &track=Adlocation1. See the comments I've addedas my explanation of the code. <script type="text/javascript">var qs = window.location.search; // haven't a clueqs = qs.substr(1); // haven't a cluevar args = qs.split("&"); // anything to the right of the & is placed in the variable argsvar arg_value = ""; // initialize arg_value as nullfor (var i = 0; i < args.length; i++){ pair = args[i].split("="); // within the args variable are 2 values divided by '=' if (pair[0] == "track") // if the first value is 'track' then do { arg_value = pair[1] + "Submitter"; // add the literal 'Submitter' to arg_value // on the right side of the variable pair // which should be 'Adlocation1' break; // end loop }}if (arg_value != "") // arg_value is not null{ // write the argument to an existing link with an ID // I'm uncertain what this is doing, but I think I want the program to go to redirect document.getElementById("Link_id").href += "?arg=" + arg_value; // or redirect // this tells the program to go to page2.html with the arguments // Question, do I put 'http://' here when I plan to go to a program at // another domain? window.location.href = "page2.html?track=" + arg_value;} // if arg_value is null, there isn't anything to do in this script</script> Have I figured it out correctly?Bill Link to comment Share on other sites More sharing options...
Synook Posted December 15, 2007 Share Posted December 15, 2007 Hmm... <script type="text/javascript">var qs = window.location.search; // gets the querystring part of the address (e.g. "?this=that"), stores it to var qsqs = qs.substr(1); // removes the "?" (e.g. "?this=that" -> "this=that")var args = qs.split("&"); // creates an array from the qs, split along the "&" symbol (e.g. "a=1&b=2" -> ["a=1","b=2"])var arg_value = ""; // initialize arg_value as nullfor (var i = 0; i < args.length; i++) //Loops for the length of args iterations{pair = args[i].split("="); // creates an array from the var=value pair (e.g. "this=that" -> ["this", "that"])if (pair[0] == "track") // if the first value is 'track' then do{arg_value = pair[1] + "Submitter"; // add the literal 'Submitter' to arg_value// on the right side of the variable pair// which should be 'Adlocation1'break; // end loop}}if (arg_value != "") // arg_value is not null{// this tells the program to go to page2.html with the argumentswindow.location.href = "page2.html?track=" + arg_value;} // if arg_value is null, there isn't anything to do in this script</script> // Question, do I put 'http://' here when I plan to go to a program at// another domain?Yes Link to comment Share on other sites More sharing options...
oneoleguy Posted December 15, 2007 Author Share Posted December 15, 2007 Wonderful!Thank you both.Bill Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.