0
votes

I'm looking for function code to trigger javascript code upon performing a search on SharePoint list

I have a SharePoint list that i have edited and added a script editor code snippet shown here to force hyperlinks in the list to open in a new tab by adding the #openinnewwindow to the end of the links. This code works great on the list when it loads. However if i perform a search the search result links do not open in a new tab. I believe if i re-trigger the code again this would allow the links to open in new tabs just not sure what to add to trigger.

` _spBodyOnLoadFunctionNames.push("rewriteLinks");

 function rewriteLinks() {
  //create an array to store all
  var anchors = document.getElementsByTagName("a");

  //loop through the array
  for (var x=0; x<anchors.length; x++) {
    //check to see if the current anchor element contain  
#openinnewwindow 
    if (anchors[x].outerHTML.indexOf('#openinnewwindow')>0) {
      //add the [target] attribute and rewrite the [href] attribute
      anchors[x].target = "_blank";
      anchors[x].href = anchors[x].href.replace(/#openinnewwindow/,'');
    }
  }
}
</script>`
1

1 Answers

0
votes

I think what you may be looking for is to have your function run after each postback. Check out solution posted here on Stackoverflow. I'm copying the relevant bits from that accepted answer:

You can use the ClientScriptManager to make a call to a function on the reload: ClientScriptManager.RegisterStartupScript(this.GetType(), "AKey", "MyFunction();", true);

http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx