Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think that the OP is asking how bit.ly converts the long URL into a shorter URL without reloading the page. This would be done with an AJAX call to a web page, that has a script running on it.</p> <p>Here is my attempt getting a similar effect on a form submit, I don't know what aspect is what you are most interested in.</p> <pre><code>&lt;script type="text/javascript"&gt; document.getElementById('formElement').onsubmit = "return requestURL();"; function requestURL() { var data = document.getElementById('formInputData').value; // get value from &lt;input type="text" id="formInputData /&gt; if (window.XMLHttpRequest) { var request=new XMLHttpRequest(); // code for IE7+, Firefox, Chrome, Opera, Safari } else { var request=new ActiveXObject("Microsoft.XMLHTTP"); // code for IE6, IE5 } request.onreadystatechange=function() { if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) { // if the request worked, output the data the server returned (PHP, .net, etc.) document.getElementById('serveroutput').innerHTML = result.responseText; } request.open("GET", "serverscript.ext?data="+data, "true"); // Setup the server request request.send(); // Send the request to the server return false; // Stops the form from submitting normally } &lt;/script&gt; </code></pre> <p>This example has no security, style, or other special stuff, but might give a better idea.<br> JSON or similar methods are probably used to get data back to JavaScript, which then styles and displays the data. I can't give specifics of what bit.ly uses, but this is the idea I would use if I tried a similar JavaScript solution.</p> <p>How did I answer the question? Did I answer something completely different? Should I expand on the answer? Do I ask too many questions? (Yes to the last question)</p> <hr> <p>In response to a comment upon pressing the space bar, that functionality can be added to the above code with the following function.</p> <pre><code>&lt;script type="text/javascript"&gt; document.getElementById('formInputData').onkeypress = function() { if(document.getElementById('formInputData').value.split('')[-1] === " ") { document.getElementById('formInputData').value = document.getElementById('formInputData').value.substring(0, str.length - 1); requestURL(); } } &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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