Retaining the Search Scope of Search Area Dropdown value during the postbacks in SharePoint

During the implementation one of my colleague faced the issue of losing the value of search scope during the postbacks, below are the two steps to include to all MasterPages

Step  I :

Create the attribute dynamically as below at delay loading include it to very end,

<script language=”javascript”>

//Set the attribute dynamically to Searchbox Area control

var e = document.getElementById(“ctl00_PlaceHolderSearchArea_ctl00_SBScopesDDL”);

var strSelectedSearchScope = e.options[e.selectedIndex].text;

var att=document.createAttribute(“onchange”);

att.value=”SetSearchScopeValue()”;

e.setAttributeNode(att);

// Retain the value of the Dropdown during the postbacks

ReSetSearchScopeValue()

</script>

Step  II :

Write the below function as early loading include it to very beginning,

function SetSearchScopeValue()

{

var c_DropDownIndex=”DropDownIndex”;

var e =  document.getElementById(“ctl00_PlaceHolderSearchArea_ctl00_SBScopesDDL”);

var c_DropDownIndexValue=e.options[e.selectedIndex].text;

document.cookie=c_DropDownIndex + “=” + c_DropDownIndexValue;

}

function ReSetSearchScopeValue()

{

var c_DropDownIndex= “DropDownIndex”;

var v_DropDownIndex=getCookie(c_DropDownIndex);

var dd = document.getElementById(“ctl00_PlaceHolderSearchArea_ctl00_SBScopesDDL”);

//dd.options[dd.selectedIndex].text=v_DropDownIndex;

for (var i = 0; i < dd.options.length; i++)

{

if (dd.options[i].text == v_DropDownIndex)

{

dd.selectedIndex = i;

break;

}

}

}

function getCookie(c_name)

{

var c_value = document.cookie;

var c_start = c_value.indexOf(” ” + c_name + “=”);

if (c_start == -1)

{

c_start = c_value.indexOf(c_name + “=”);

}

if (c_start == -1)

{

c_value = null;

}

else

{

c_start = c_value.indexOf(“=”, c_start) + 1;

var c_end = c_value.indexOf(“;”, c_start);

if (c_end == -1)

{

c_end = c_value.length;

}

c_value = unescape(c_value.substring(c_start,c_end));

}

return c_value;

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s