Friday, May 6, 2011

Replacing URL in HTML using JQuery

Scenario:
Sometimes if a list is currupted, Allitems.aspx's HTML starts acting weird and we dont want to spend too much time to dig deep into the problem but want a quick fix and endup fixing the HTML. Same goes here.

Solution:
Following code change the URLs in a page to a predefined URL.
<script type="text/javascript">

 if(typeof jQuery=="undefined")
 {
  var jQsrc="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.4.min.js";
  document.write(unescape("%3Cscript src='"+jQsrc+"' type='text/javascript'%3E%3C/script%3E"));
 }
</script>

<script type="text/javascript">

$(document).ready(function() {
  //find all hyperlinks in the table holding calendar links and
  //change them to point at the display page instead of the edit page
  $("table[id^='CalViewTable']").find("a[href*='OldForm.aspx']").each(function(){
  var originalUrl = $(this).attr("href");
  var newUrl = originalUrl.replace("OldForm.aspx", "NewForm.aspx");
$(this).attr("href", newUrl);
});
});

</script>
Ref: http://social.msdn.microsoft.com/Forums/en-US/sharepointcustomization/thread/ee519099-e685-4560-9c18-70e4aa62eb39