stephenjbell

Custom Field - Make title into URL, unhide field (dotCMS)

Mar 26th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.91 KB | None | 0 0
  1. <script>window.jQuery || document.write('<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">\x3C/script>')</script>
  2. <script type="text/javascript">
  3.        
  4.         // make sure jquery doesn't break dojo
  5.         jQuery.noConflict();
  6.         jQuery(document).ready(function($) {
  7.         sourceInputName = "#title";
  8.                 destInput = $("#urlTitle");
  9.  
  10.                 // function to convert text to a proper url slug
  11.                 function cleanUrl(url){
  12.                         url = url.toLowerCase();
  13.                         url = url.replace(/@.*$/g,""); /* Get rid of @oc.edu */
  14.                         url= url.replace(/^\s+|\s+$/g,"");
  15.                         url = url.replace(/[^a-zA-Z 0-9]+/g,' ');      
  16.                         url = url.replace(/\s/g, "-");                
  17.                         while(url.indexOf("--") > -1){
  18.                                 url = url.replace("--",'-');  
  19.                         }                              
  20.                         url = url.replace(/^-/,"");
  21.                         url = url.replace(/-$/g,"");
  22.                         return url;
  23.                 }
  24.                
  25.                 function updateUrlTitle(){
  26.                         var myUrl = cleanUrl($(sourceInputName).val());
  27.                         destInput.val(myUrl);
  28.                 }
  29.                
  30.                 updateUrlTitle(); // Run initially
  31.                
  32.                 // when title is changed, update fields
  33.                                 $(document.body).on('change',sourceInputName,function(){
  34.                         updateUrlTitle();
  35.                         console.log("sourceInput has changed");
  36.                 });
  37.                
  38.                
  39.                 // Make it visible by taking it out of the page and putting it back in
  40.                 destInput.detach().attr("type","text").appendTo("#urlTitle_field");
  41.                
  42.         });
  43.  
  44. </script>
Advertisement
Add Comment
Please, Sign In to add comment