1

First of all, excuse my terminology as I'm not an ASP.net MVC framework developer. My company is using an ASP.net MVC 5 framework. I'm developing the analytics code using Adobe DTM for this new framework. The issue I'm having is I recently worked on an Angular/Node.js implementation where my JavaScript files were only loaded initially and then ran on every view without being reloaded allowing me to keep track of states etc. I'm now working at a new company and they are using a ASP.net MVC 5 framework, but the JavaScripts are being reloaded every view. From what the developers are telling me, it is a hybrid where some pages use a controller and other pages don't. Is there a way to load JavaScript one time (initial load) and keep the JavaScript running (not destroying objects/variables)?

Thanks!

1
  • 2
    You'd have to convert the application to a SPA (single page application) to keep the same javascript running. Every click on an anchor <a> tag/node will reset the page and cause everything to be reloaded into the browser (likely from cache, but all variables etc reset). Most MVC apps are either SPA or use simple links to pages to keep them lightweight and stateless. It's unlikely a 'normal' business application written in MVC would be a good candidate to be converted to SPA. I don't know Adobe DTM - can you hook into the C# code (server-side) ? Commented Jul 31, 2015 at 21:44

1 Answer 1

1

The only way without using a SPA, or switching analytics would be to store the variables and then set the value on page load with the stored value.

Depending on what Adobe DTM requires, you could use localStorage or sessionStorage, the latter will most likely be applicable, as it will store your set variables throughout the current browsing session, various implementations:

$(selector).click(function() {
    sessionStorage.setItem('name', 'value');
});

sessionStorage.setItem('myObject', JSON.Stringify(myObject));

Using window.name as a hack to store global information. You can assign a value to window.name, which will persist throughout the session, provided the user stays on the same tab/window. This is a cross-domain solution as well, although WebStorage is likely more reliable in your case.

window.name = JSON.Stringify({ clicked: true });
window.name = "myString";

Window.name only stores a string, if you need to store a complex object or multiple values you will have to use JSON.Stringify.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.