Paul's profileAutoSpongePhotosBlogLists Tools Help

Blog


    July 02

    Cleanup Hyperlinks in Calculated Columns

    A common request from end users:  "Can we make a calculated column of hyperlinks that don't display the whole url?"

    Because MOSS filters "dangerous code" from metadata columns, calculated columns can not resolve to something with code in it and the Excel function "HYPERLINK" does not work in MOSS.  So, what can we do?

    Admins and power users can clean this up with SPD or custom web parts.  However, the end user has fewer options.  If the end user has the Edit Page permission and access to the Content Editor Web Part, they can use some javascript to clean up the links.  If you don't normally allow that, you may want to add the second technique's script to your default.master as it will clean up any ugly links on your page.

    Technique 1

    Let's say, you have a bunch of document names in a list and you want to create a url to another site using a calculated column like this:  ="http://google.com/"&Title

    We know the "base URL" and we want to dynamically separate the appended portion from the base (e.g., displaying http://www.google.com/labs as labs),  so we add this script to the list view page using a CEWP:

    <script type="text/javascript">

    _spBodyOnLoadFunctionNames.push("cleanLinks");

     

    function cleanLinks(){

      var baseURL = "http://google.com/";

      var len = baseURL.length;

      var links = document.getElementsByTagName("a");

      for (var i=0; i < links.length; i++) {

        if (links[i].href && links[i].href.substring(0,len) == baseURL){

          if (links[i].innerText==undefined) {

          links[i].textContent = links[i].textContent.substring(len);

          }

          else{

          links[i].innerText = links[i].innerText.substring(len); //for IE

          }

        }

      }

    }

     

    //-->

    </script>


    Just replace the value of the variable 'baseURL' with whatever you use for a base in your calculated column.  If you also want to clean up the text (like in the case of url-encoded file names or underscores that replace spaces), you'll need the replace() method in both lines that replace the display text (one for Firefox and one for IE).

    Technique 2

    In this case, we can not predict the "base URL."  So we will clean up our links by replacing the url with the word "link" for any tag using the href attribute as its display text.

    <script type="text/javascript">

    _spBodyOnLoadFunctionNames.push("cleanLinks");

     

    function cleanLinks(){

      var links = document.getElementsByTagName("a");

      for (var i=0; i < links.length; i++) {

        if (links[i].href && links[i].href == links[i].innerText){

          links[i].innerText = "link";

          }else{

          if (links[i].href && links[i].href == links[i].textContent){

          links[i].textContent = "link";

          }

        }

      }

    }

     

    //-->

    </script>


    A slightly fancier version of that technique adds an image using some embedded CSS:

    <STYLE TYPE="text/css" MEDIA=screen>

    <!--

    .mylink

    {

        background-image: url(/_layouts/images/link.gif);

        background-repeat: no-repeat;

        font-family: tahoma,sans-serif;

        font-size: 8pt;

        padding-left: 25px;

    }

    -->

    </STYLE>

    <script type="text/javascript">

    _spBodyOnLoadFunctionNames.push("cleanLinks");

     

    function cleanLinks(){

      var links = document.getElementsByTagName("a");

      for (var i=0; i < links.length; i++) {

        if (links[i].href && links[i].href == links[i].innerText){

          links[i].parentNode.className="mylink";

          links[i].innerText = "link";

          }else{

          if (links[i].href && links[i].href == links[i].textContent){

          links[i].parentNode.className="mylink";

          links[i].textContent = "link";

          }

        }

      }

    }

    //-->

    </script>

    Comments (1)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    No namewrote:
    http://www.batterygoshop.co.uk/mitac/bp-8050-battery.htm mitac bp-8050 battery ,
    http://www.batterygoshop.co.uk/mitac/minote-8080-battery.htm mitac minote 8080 battery ,
    http://www.batterygoshop.co.uk/mitac/minote-8090-battery.htm mitac minote 8090 battery ,
    http://www.batterygoshop.co.uk/mitac/8375-battery.htm mitac 8375 battery ,
    http://www.batterygoshop.co.uk/mitac/e2000-battery.htm mitac e2000 battery ,
    http://www.batterygoshop.co.uk/mitac/8x75-battery.htm mitac 8x75 battery ,
    http://www.batterygoshop.co.uk/mitac/8575-battery.htm mitac 8575 battery ,
    http://www.batterygoshop.co.uk/mitac/bp-8089-battery.htm mitac bp-8089 battery ,
    http://www.batterygoshop.co.uk/mitac/bp-8089x-battery.htm mitac bp-8089x battery ,
    http://www.batterygoshop.co.uk/mitac/bp-8389-battery.htm mitac bp-8389 battery ,
    http://www.batterygoshop.co.uk/mitac/bp-8089p-battery.htm mitac bp-8089p battery ,
    http://www.batterygoshop.co.uk/mitac/e680-battery.htm mitac e680 battery ,
    http://www.batterygoshop.co.uk/mitac/winbook-w200-battery.htm mitac winbook w200 battery ,
    http://www.batterygoshop.co.uk/mitac/winbook-w235-battery.htm mitac winbook w235 battery ,
    http://www.batterygoshop.co.uk/mitac/bp-8011-battery.htm mitac bp-8011 battery ,
    http://www.batterygoshop.co.uk/mitac/bp-8x11-battery.htm mitac bp-8x11 battery ,
    http://www.batterygoshop.co.uk/mitac/black-bp-8011-battery.htm mitac black bp-8011 battery ,
    http://www.batterygoshop.co.uk/mitac/bp-8x99-battery.htm mitac bp-8x99 battery ,
    http://www.batterygoshop.co.uk/mitac/bp-8599-battery.htm mitac bp-8599 battery ,
    http://www.batterygoshop.co.uk/nec/op-570-75303-battery.htm nec op-570-75303 battery ,
    http://www.batterygoshop.co.uk/nec/pc-vp-wp25-battery.htm nec pc-vp-wp25 battery ,
    http://www.batterygoshop.co.uk/nec/pc-vp-wp44-battery.htm nec pc-vp-wp44 battery ,
    http://www.batterygoshop.co.uk/nec/op-570-75901-battery.htm nec op-570-75901 battery ,
    http://www.batterygoshop.co.uk/nec/m300-battery.htm nec m300 battery ,
    http://www.batterygoshop.co.uk/nec/m500-battery.htm nec m500 battery ,
    http://www.batterygoshop.co.uk/sony/pcga-bp2sa-battery.htm sony pcga-bp2sa battery ,
    http://www.batterygoshop.co.uk/sony/pcga-bp2s-battery.htm sony pcga-bp2s battery ,
    http://www.batterygoshop.co.uk/sony/pcga-bp2t-battery.htm sony pcga-bp2t battery ,
    http://www.batterygoshop.co.uk/sony/pcga-bp2v-battery.htm sony pcga-bp2v battery ,
    http://www.batterygoshop.co.uk/sony/pcg-z1-battery.htm sony pcg-z1 battery ,
    http://www.batterygoshop.co.uk/sony/pcg-v505-battery.htm sony pcg-v505 battery ,
    http://www.batterygoshop.co.uk/sony/pcga-bp2e-battery.htm sony pcga-bp2e battery ,
    http://www.batterygoshop.co.uk/sony/vgp-bp2ea-battery.htm sony vgp-bp2ea battery ,
    http://www.batterygoshop.co.uk/sony/pcg-gr100-battery.htm sony pcg-gr100 battery ,
    http://www.batterygoshop.co.uk/sony/pcg-gr150-battery.htm sony pcg-gr150 battery ,
    http://www.batterygoshop.co.uk/sony/pcg-gr250-battery.htm sony pcg-gr250 battery ,
    http://www.batterygoshop.co.uk/sony/pcg-gr270-battery.htm sony pcg-gr270 battery ,
    http://www.batterygoshop.co.uk/sony/vgp-bps2a-battery.htm sony vgp-bps2a battery ,
    http://www.batterygoshop.co.uk/sony/vgp-bps2b-battery.htm sony vgp-bps2b battery ,
    http://www.batterygoshop.co.uk/sony/vgp-bps2-battery.htm sony vgp-bps2 battery ,
    http://www.batterygoshop.co.uk/sony/vgp-bps2b-battery.htm sony vgp-bps2b battery ,
    http://www.batterygoshop.co.uk/sony/vgp-bps2c-battery.htm sony vgp-bps2c battery ,
    http://www.batterygoshop.co.uk/sony/vgp-bpl2c-battery.htm sony vgp-bpl2c battery ,
    http://www.batterygoshop.co.uk/toshiba/satellite-1200-battery.htm toshiba satellite 1200 battery ,
    Mar. 11

    Trackbacks

    The trackback URL for this entry is:
    http://autosponge.spaces.live.com/blog/cns!D7F85948C20F0293!446.trak
    Weblogs that reference this entry
    • None