You might be wondering why you would want to use this code instead of just editing the web part naturally in SharePoint. If you use one page to display multiple sets of information and you want to rotate through the WP icon, this is how. For example:
States.aspx needs to show information for several states.
"States.aspx?s=Colorado" should show the Colorado state flag (Colorado.png) at the top of the page and "States.aspx?s=Arizona" should show Arizona's flag (Arizona.png).
Make sure you have the .png's in the same folder as State.aspx and use this code to swap out the default WP icon:<script type="text/javascript">This code goes into a Content Editor Web Part anywhere on the page.
var aryV = location.search.replace("?","").split("&");
var i=0
while (i < aryV.length)
{ if (aryV[i].indexOf("s=") >= 0)
{ strState = aryV[i].substr(aryV[i].indexOf("=")+1).replace(/%20/g, " ");
strState = strState.replace(/\+/g, " ");
}
i+=1;
}
var theImgs = document.getElementsByTagName("IMG");
var x=0;
while (x < theImgs.length)
{ try
{ if (theImgs[x].src.search("wpicon.gif") >= 0)
{ theImgs[x].setAttribute("src", s + ".png"); break; }
}
catch(err){}
x+=1;
}
</script>


0 comments:
Post a Comment