Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>The Art of Links with Javascript</h2> <p><em>A handy little answer by howderek</em></p> <p><strong>JSFiddle: <a href="http://jsfiddle.net/howderek/RrS8L/22/" rel="nofollow">http://jsfiddle.net/howderek/RrS8L/22/</a></strong></p> <p><strong>Step 1: Creating links that pass data with JavaScript</strong></p> <p>You already had a nice little function for generating a URL, but it needed a little modification in order to be more easily parsed. I made everything come after the <code>?</code> in order to better differentiate between the location and the data being passed to that page, as well as organizing the code a little nicer. Here is my modified <code>showURL</code></p> <pre><code>function showURL() { var d1 = $("#country").find(":selected").attr("value"), d2 = $("#sleeps").find(":selected").attr("value"), d3 = $("#pets").find(":selected").attr("value"), url = encodeURI('http://jsfiddle.net/howderek/RrS8L/20/show/light/?country=' + d1 + "&amp;sleeps=" + d2 + "&amp;pets=" + d3); alert(url); document.location = url; } </code></pre> <p>I got rid of your inline onclick, I made the form a div rather than a form (to prevent submission), and I made the <code>input type="submit"</code> into a <code>button</code> </p> <p>I also created a paragraph to display results.</p> <pre><code>&lt;p id="results"&gt;There was no information passed through the URL.&lt;/p&gt; &lt;div id="dogform"&gt; &lt;select class="dropdown" id="country"&gt; &lt;option value="England"&gt;England&lt;/option&gt; &lt;option value="France"&gt;France&lt;/option&gt; &lt;option value="the United States of America"&gt;Murica&lt;/option&gt; &lt;option value="Russia"&gt;Russia&lt;/option&gt; &lt;/select&gt; &lt;select class="dropdown" id="sleeps"&gt; &lt;option value="a few"&gt;Less than 4 hours&lt;/option&gt; &lt;option value="four to eight"&gt;4-8 hours&lt;/option&gt; &lt;option value="many"&gt;More than 8 hours&lt;/option&gt; &lt;/select&gt; &lt;select class="dropdown" id="pets"&gt; &lt;option value="nonexistent"&gt;None&lt;/option&gt; &lt;option value="beloved"&gt;One&lt;/option&gt; &lt;option value="many"&gt;Lots&lt;/option&gt; &lt;/select&gt; &lt;button id="submit"&gt;Do stuff.&lt;/button&gt; &lt;/div&gt; </code></pre> <p><strong>Step 2: Reading URLs with JavaScript</strong></p> <p>I wrote a function a while back to read URL keys. It returns an object made of strings that are named after the keys. This allows you to read the values stored in the URL easily. </p> <pre><code>function urlAnalyze(url) { if (url == undefined) { var url = document.location.toString(); } url = unescape(url); //seperates the parts of the url var parts = url.split("?"); //splits into sperate key=values if (parts[1] == undefined) { return 1; } var keyValues = parts[1].split("&amp;"); var key = function () {} keyValues.forEach(function (keyValue) { var keyAndValue = keyValue.split("="); key[keyAndValue[0]] = keyAndValue[1]; }); return key; } $('#submit').click(showURL); keys = urlAnalyze(); if (keys['country']) { $('#results').html('In ' + keys['country'] + ', people like to sleep for ' + keys['sleeps'] + ' hours after walking their ' + keys['pets'] + ' pets.'); } </code></pre> <p>I also made it easy to see what the URL inside of the JSFiddle is:</p> <pre><code>$('#results')[0].innerHTML += '&lt;br&gt;&lt;br&gt;URL of this: ' + document.location; </code></pre> <p>You will have to replace the links to JSFiddle with the appropriate links to your website, but I believe that this should answer your question. </p> <p>Here is an example of my usage of my function <code>URLTools</code> in one of my projects: <a href="http://howderek.com/projects/WordEngine/?q=this%20value%20was%20passed%20through%20the%20URL%20and%20read%20by%20javascript" rel="nofollow">http://howderek.com/projects/WordEngine/?q=this%20value%20was%20passed%20through%20the%20URL%20and%20read%20by%20javascript</a></p> <p>Enjoy!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload