0

I am wondering if it is possible to name raygun variables using the BundleConfig file.

SO in my bundle I add my scripts as so:

bundles.Add(new ScriptBundle("~/master.js").Include(
                "~/static/js/json2.js",
                "~/static/js/jquery/jquery-ui.min.js",
                "~/static/js/jquery/jquery.simplemodal.js",
                "~/static/js/jquery/jquery.maskedinput.js",
                "~/static/js/jquery/jquery.validate.js",
                "~/static/js/jquery/jquery.inlineEditing.js",
                "~/static/js/jquery/jquery.contextmenu.js",
                "~/Scripts/raygun.js"));

I was wondering if it is possible to at all name my raygun.js reference as rg4js such that i could call it in my JS file where i want to use it. This is attempting to achieve the same thing as what they (Raygun) mention in their documentation by adding the following two script tags in the head and body, respectively.

<script type="text/javascript">
  !function(a,b,c,d,e,f,g,h){a.RaygunObject=e,a[e]=a[e]||function(){
  (a[e].o=a[e].o||[]).push(arguments)},f=b.createElement(c),g=b.getElementsByTagName(c)[0],
  f.async=1,f.src=d,g.parentNode.insertBefore(f,g),h=a.onerror,a.onerror=function(b,c,d,f,g){
  h&&h(b,c,d,f,g),g||(g=new Error(b)),a[e].q=a[e].q||[],a[e].q.push({
  e:g})}}(window,document,"script","//cdn.raygun.io/raygun4js/raygun.min.js","rg4js");
</script>

<script type="text/javascript">
  rg4js('apiKey', 'paste_your_api_key_here');
  rg4js('enableCrashReporting', true);
</script>

As you can see, the first script defines the name (rg4js) whilst the latter one then uses it as a function name. Can I somehow achieve a similar thing with BundleConfig.cs, or would i need to instead insert those script tags into the shared views that act as templates for other parts of the website?

MethodMan
  • 18,625
  • 6
  • 34
  • 52
SomeStudent
  • 2,856
  • 1
  • 22
  • 36
  • I assume the that the asp.net bundler concatenates in order since these scripts typically rely on global variables. That said, you should avoid this kind of structuring it is very brittle. You should also avoid inline scripts. – Aluan Haddad Dec 06 '17 at 14:48
  • It is a legacy system, so as far as the bundling goes it is what is used at the moment. As for the second point, this is why I want to know if i can do it in a way that avoids inline scripts ingeneral. – SomeStudent Dec 06 '17 at 14:50
  • OK, I understand that you don't want to refactor it. You can place the contents of both script tags into a single `.js` file and add it below raygun in your `BundleConfig.cs` – Aluan Haddad Dec 06 '17 at 14:51
  • So in essence have a separate js file that is referenced that sets up that variable? Would it then be available elsewhere in my code base such that i could also call that function wherever i need to have logging? I would certainly appreciate an example if that is okay with you – SomeStudent Dec 06 '17 at 14:54
  • I don't have a project handy where I can test how the bundling works in a WebForms app or else I would add an answer. However, yes, it will be available as a global variable on any page the bundle is loaded on after it is loaded. – Aluan Haddad Dec 06 '17 at 14:56
  • I just realized that your approach is problematic because the first script actually loads raygun itself but your bundle also contains it. You are loading it twice which is not good. – Aluan Haddad Dec 06 '17 at 15:08
  • Just remove `raygun.js` from your bundle completely and place the first script tag in the `` as specified in the [documentation](https://raygun.com/docs/languages/javascript). Then any script you load can access it as a global since ASP.NET will render the script bundle after after the head tag. – Aluan Haddad Dec 06 '17 at 15:11
  • Gotcha, that makes sense. Is it possible to avoid needing to include it in a or is it pretty much mandatory? – SomeStudent Dec 06 '17 at 15:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/160628/discussion-between-aluan-haddad-and-somestudent). – Aluan Haddad Dec 06 '17 at 15:19

0 Answers0