简体   繁体   中英

ASP.net MVC framework and caching and executing javascript

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM