Wednesday, March 14, 2007

Javascript in the Onload Event for a SharePoint Page

The simple way to add JavaScript behaviours to a SharePoint page is to add a script block with the for and event attributes set to the relevant object and event.

For instance, to insert a value from the querystring into the page title, add the following script to the top of the PlaceHolderMain Content Place Holder block:


    <script type="text/javascript" language="javascript">
    function getQueryStringValue(qsName) {
        
var querystring = window.location.search.substring(1);
        var 
qsPair querystring.split("&");
        for 
(var i=0;i<qsPair.length;i++) {
            
var pair qsPair[i].split("=");
            if 
(pair[0].toLowerCase() == qsName.toLowerCase()) {
                  
return pair[1];
            
}
        } 
        
return '';
    
}    
    </script>
    <script type
="text/javascript" language="javascript" 
for="window" event="onload">
        
document.title 'Details for ' + getQueryStringValue('supplier') + document.title;
    
</script>

1 comment:

Menerva said...

Great post, but is there a way to use SharePoint Model Object in javascript ?