ScriptResx.ashx in SharePoint
By Anatoly Mironov
In my previous post I showed a little proof-of-concept for an httphandler which I want to use to dynamically get the localization resources from SharePoint as javascript object. But wait a moment. How does SharePoint handle localization on client? When you look in Script tab in Chrome dev tools, you’ll find:
ScriptResource.axd
It is added to the server when deployed. See a good introduction to WebResource.axd and ScriptResource.axd by Brian Chavez.
ScriptResx.ashx
That was new. Try to google it. You won’t find much results. Wait a sec, so there is actually the localization httphandler in SharePoint which I wanted to do? Can’t be true, can it? Let’s look closer. So the url for this javascript file is:
/\_layouts/ScriptResx.ashx?culture=nb-no&name=SP.Res
```It is added with RegisterSod:
_EnsureJSNamespace(‘SP’); SP.Res={lcid:‘1044’,autocompleteAccessibleMenuName:‘Autofullf\u00F8r-meny’….
[![](https://sharepointkunskap.files.wordpress.com/2012/02/scriptresx-ashx-002.png "scriptresx.ashx-002")](https://sharepointkunskap.files.wordpress.com/2012/02/scriptresx-ashx-002.png) Very nice. But where resides this SP.Res?... Perhaps, it isn't a file, rather a name, that the javascript code is encapsulated within. The javascript object is called SP.Res. By the way, [a nice usage of it is to define the current language](http://sharepoint.stackexchange.com/questions/6915/get-the-current-ui-language-with-ecmascript):
var lang = SP.Res.lcid;
<%@ WebHandler Language=“C#” Class=“Microsoft.SharePoint.ApplicationPages.ScriptResxHandler,Microsoft.SharePoint.ApplicationPages,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c” %>
##### Usage for other and custom .resx files
Wow, those who read all this, you'll be awarded: You can actually load any localization files with ScriptResx.ashx (anything which can be found in **14/Resources/**\-folder, core, spscore and much more, or your own custom resx-files). Just add your script:
##### Get Culture dynamically
ScriptResx.ashx depends on culture name. Sure we can put this directly in the script tag. But what if you can't know the current language of the site? It is not so difficult actually, just get the current culture name in the asp.net markup:
Update: An even better solution is to use `L_Menu_LCID` (found on [Ruud Heemskerk's Blog](http://ruudheemskerk.net/archive/2010/08/03/helpful-sharepoint-javascript-functions.aspx)):
##### Resolving name conflict
If you load multiple localization files, all will use "Res" name for the localization object and override the latest localizations. To solve it we must catch the Res object and encapsulate it within another object which we have control over. Here we can use many techniques, among them [ExecuteOrDelayUntilScriptLoaded (with RegisterSod and LoadSod) from SharePoint](http://blog.mastykarz.nl/dynamically-loading-javascript-sandbox/ "See the best explanation of SOD in Sharepoint and many examples by mastykarz"), we can use [head.js to load and run a callback](http://headjs.com/). But the simplest way is to use [jQuery.getScript function](http://api.jquery.com/jQuery.getScript/): \[sourcecode language="javascript"\] jQuery.getScript("/\_layouts/scriptresx.ashx?culture=nb-no&name=Takana") .done(function() { Takana = {}; Takana.Res = Res; }) .fail(function() {console.log("error");}); \[/sourcecode\]
##### Update 2013-02-01
I found a better way to resolve the name conflict for resx-files loaded with scriptresx.ashx. Just update the classFullName resheader in your resx file. Details in my new post:
###### [JavaScript Localization in SharePoint](https://sharepointkunskap.wordpress.com/2013/02/01/javascript-localization-in-sharepoint/ "Permalink to JavaScript Localization in SharePoint")
## Comments from Wordpress.com
####
[Mario]( "mario@delafini.com") - <time datetime="2014-07-21 16:06:21">Jul 1, 2014</time>
Hello Anatoly, I tested with Safari on windows. You can try that yourself if you want. ;-) Kind regards, Mario
<hr />
####
[Anatoly Mironov](http://chuvash.eu "mirontoli@gmail.com") - <time datetime="2014-07-21 15:55:51">Jul 1, 2014</time>
Hi,Mario! It should work in Safari, as it is a standard ajax call. Unfortunately I do not have access to a Mac with a Safari. Perhaps it is some authentication issue in the ajax call. I'll try to ask somebody who does have a Mac to try it out.
<hr />
####
[Anatoly Mironov](http://chuvash.eu "mirontoli@gmail.com") - <time datetime="2014-07-22 12:15:24">Jul 2, 2014</time>
I have tested it in Safari on Windows (using the Firebug Lite for Safari). It works. Please pay attention to the namespace in the resx file that defines the name of the javascript variable which contains all the resource strings. Read more in my other blog post: [JavaScript Localization in SharePoint](/2013/02/01/javascript-localization-in-sharepoint/).
<hr />
####
[Mario]( "mario@delafini.com") - <time datetime="2014-07-22 16:45:03">Jul 2, 2014</time>
Hello Anatoly, Thanks. It seems that my issue is that the script in Safari isn't loaded yet before called. And this issue is not there in IE. So I tried window.load and document.ready but this is giving me a headache because one does not work in all browsers. Do you have a suggestion how to use a script that fires after document or DOM has loaded completely? Kind regards, Mario
<hr />
####
[Mario]( "mario@delafini.com") - <time datetime="2014-07-22 17:02:09">Jul 2, 2014</time>
Hello Anatoly, Just to be complete. I use the following code. Which runs fine in IE (all 3 alerts) But will only return the first 2 alerts in Safari: window.onload=function(){ var thisUserID = $().SPServices.SPGetCurrentUser({ fieldName: "SipAddress", debug: false }); alert(thisUserID); // Get Current language information function GetCurrentCulture(){ var lang = document.getElementsByTagName('html')\[0\].getAttribute('lang'); var strs = lang.split('-'); return strs\[0\].toString() + '-' + strs\[1\].toString().toUpperCase() ; } var currentCult = GetCurrentCulture(); alert(currentCult); var search\_query = "/\_layouts/ScriptResx.ashx?culture=" + currentCult + "&name=InCompanyApps"; s = document.createElement('script'); s.src = search\_query; document.getElementsByTagName('head')\[0\].appendChild(s); // Get tag text according to current culture var myRes = Res; alert("Localized Value is : " + myRes\['new\_Item'\]); var myID = fd.getUrlParam(window.location.href, 'ID'); //alert(myID); }
<hr />
####
[Anatoly Mironov](http://chuvash.eu "mirontoli@gmail.com") - <time datetime="2014-07-22 17:31:11">Jul 2, 2014</time>
Maybe, Safari treats the script tag differently, Try setting async=false: s.async="false". You could also provide a new onload event for the new script element: s.onload = function() { alert(Res\['new\_Item'\]);}; Another thing, you shouldn't override window.onload, it may have dangerous consequences.
<hr />
####
[Mario]( "mario@delafini.com") - <time datetime="2014-07-22 17:51:43">Jul 2, 2014</time>
Hello Anatoly, Now I have included your suggestions which works fine in Safari but not in IE: var thisUserID = $().SPServices.SPGetCurrentUser({ fieldName: "SipAddress", debug: false }); alert(thisUserID); // Get Current language information function GetCurrentCulture(){ var lang = document.getElementsByTagName('html')\[0\].getAttribute('lang'); var strs = lang.split('-'); return strs\[0\].toString() + '-' + strs\[1\].toString().toUpperCase() ; } var currentCult = GetCurrentCulture(); alert(currentCult); var search\_query = "/\_layouts/ScriptResx.ashx?culture=" + currentCult + "&name=InCompanyApps"; s = document.createElement('script'); s.src = search\_query; s.async="false"; document.getElementsByTagName('head')\[0\].appendChild(s); s.onload = function() { alert(Res\['new\_Item'\]);};
<hr />
####
[Anatoly Mironov](http://chuvash.eu "mirontoli@gmail.com") - <time datetime="2014-07-22 17:55:22">Jul 2, 2014</time>
That's strange. Perhaps the only thing to do is to keep the code that works in IE, and implement special code for Safari. In SharePoint javascript library, there is a variable called browseris. There you can check if it is Safari.
<hr />
####
[Accédez à vos fichiers de ressources selon la langue courante avec SharePoint 2010 | The collaboration corner](http://thecollaborationcorner.com/2014/08/04/accedez-a-vos-fichiers-de-ressources-selon-la-langue-courante-avec-sharepoint-2010/ "") - <time datetime="2014-08-04 19:36:11">Aug 1, 2014</time>
\[…\] la lecture du très bon article, voici la solution finale que j’ai utilisée avec SharePoint \[…\]
<hr />
####
[Anatoly Mironov](http://chuvash.eu "mirontoli@gmail.com") - <time datetime="2014-07-22 09:13:17">Jul 2, 2014</time>
Allright, I see. Then I can try it, too.
<hr />
####
[Mario]( "mario@delafini.com") - <time datetime="2014-07-21 11:29:13">Jul 1, 2014</time>
Hello Anatoly, We use this approach on our sharepoint pages which works fine in IE. But recently we found out it does not work in Safari. Do you have any idea how to fix this? Kind regards, Mario
<hr />
####
[Abdul Raoof]( "raoof@sysberries.com") - <time datetime="2018-02-12 16:51:40">Feb 1, 2018</time>
I tried the scriptresx.ashx in sharepoint 2016. Its always failing. Please find my code function getResourceVal() { var resourceUrl = '/\_layouts/15/ScriptResx.ashx?culture=' + currentUiCluture + '&name=ADBWC'; $.getScript(resourceUrl) .done(function () { console.log(Res); }) .fail(function (jqxhr, settings, exception) { console.log(exception); }); } The error I am getting is parse error :"SyntaxError: Unexpected identifier↵ at eval ()↵ " So when I checked the result I could find a if else statement because of which it is failing to parse. please find var Res={search directory:'\\u062F\\u0644\\u064A\\u0644 \\u0627\\u0644\\u0628\\u062D\\u062B',search Results:'\\u0646\\u062A\\u0627\\u0626\\u062C \\u0627\\u0644\\u0628\\u062D\\u062B',select city:'\\u0625\\u062E\\u062A\\u0631 \\u0627\\u0644\\u0645\\u062F\\u064A\\u0646\\u0629',share:'\\u0646\\u0634\\u0631',show selected companies:'\\u0623\\u0638\\u0647\\u0631 \\u0627\\u0644\\u0634\\u0631\\u0643\\u0627\\u062A \\u0627\\u0644\\u0645\\u062E\\u062A\\u0627\\u0631\\u0629',showing results:'\\u0623\\u0638\\u0647\\u0631 \\u0627\\u0644\\u0646\\u062A\\u0627\\u0626\\u062C',street:'\\u0627\\u0644\\u0634\\u0627\\u0631\\u0639',telephone:'\\u0627\\u0644\\u0647\\u0627\\u062A\\u0641',telex:'\\u0627\\u0644\\u062A\\u0644\\u0643\\u0633',unified No:'\\u0627\\u0644\\u0631\\u0642\\u0645 \\u0627\\u0644\\u0645\\u0648\\u062D\\u062F',you have searched for:'\\u0644\\u0642\\u062F \\u0642\\u0645\\u062A \\u0628\\u0627\\u0644\\u0628\\u062D\\u062B \\u0639\\u0646'}; ↵if (typeof(Sys)!='undefined' && Sys && Sys.Application) { Sys.Application.notifyScriptLoaded(); } Any help will be appreciated. I am stuck.
<hr />