title: Word count bookmarklet
body: <p>I'm trying to create a word count (for any page) bookmarklet without loading an external file. In short I want to click the bookmarklet and then be able to drag and select text on the screen and get an alert with the number of words selected. I have put this together to get the correct functionality but am stumbling on converting to bookmarklet:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body onmouseup="countWords()"&gt;     
  &lt;article id="page1"&gt;
      &lt;h1&gt;Home 2&lt;/h1&gt;
      &lt;p&gt;Welcome 2&lt;/p&gt;
      &lt;script type="text/javascript"&gt;
  function countWords() {
  var selectedText = document.activeElement;
  var selection = selectedText.value.substring(selectedText.selectionStart, selectedText.selectionEnd);
  words = selection.match(/[^\s]+/g).length;
  if (words !== "") {
      alert(words);
  }
  }
  &lt;/script&gt;
      &lt;div&gt;&lt;textarea&gt;&lt;/textarea&gt;&lt;/div&gt;
  &lt;/article&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>First issue: I may be barking up the wrong tree but I wanted to attach the onmouseup to the activeElement but am at a loss as to how to do this. </p>
  
  <p>Second issue: Can I insert this in a bookmarklet without the use of an external file?</p>
  
  <p>Any assistance would be greatly appreciated. </p>
  
  <p>Best,</p>
  
  <p>Tamler</p>
  
  <p>Escape characters... That was the issue.</p>
  
  <p>Here is a working example:</p>
  
  <pre><code>&lt;a href="javascript:(document.onmouseup=function(){var selectedText=document.activeElement;var selection=selectedText.value.substring(selectedText.selectionStart,selectedText.selectionEnd);words=selection.match(/[^\s]+/g).length;if(words!==&amp;quot;&amp;quot;){alert(words)}})();" target="_blank"&gt;Word Count&lt;/a&gt;
  </code></pre>