简体   繁体   English

ASP.net MVC框架以及缓存和执行JavaScript

[英]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. 首先,请原谅我的术语,因为我不是ASP.net MVC框架开发人员。 My company is using an ASP.net MVC 5 framework. 我的公司正在使用ASP.net MVC 5框架。 I'm developing the analytics code using Adobe DTM for this new framework. 我正在使用Adobe DTM针对此新框架开发分析代码。 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. 我遇到的问题是我最近在Angular / Node.js实现中进行工作,在该实现中,我的JavaScript文件仅最初加载,然后在每个视图上运行而没有重新加载,从而使我能够跟踪状态等。我现在在一家新公司,他们正在使用ASP.net MVC 5框架,但是每个视图都将重新加载JavaScript。 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)? 有没有办法一次加载JavaScript(初始加载)并保持JavaScript运行(不破坏对象/变量)?

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. 不使用SPA或切换分析的唯一方法是存储变量,然后使用存储的值设置页面加载时的值。

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: 根据Adobe DTM的要求,您可以使用localStoragesessionStorage ,后者很可能适用,因为它将在当前浏览会话中存储设置的变量,并实现以下各种实现:

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

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

Using window.name as a hack to store global information. 使用window.name作为黑客来存储全局信息。 You can assign a value to window.name , which will persist throughout the session, provided the user stays on the same tab/window. 您可以为window.name分配一个值,只要用户停留在同一选项卡/窗口中,该值将在整个会话中持续存在。 This is a cross-domain solution as well, although WebStorage is likely more reliable in your case. 这也是一个跨域解决方案,尽管在您的情况下WebStorage可能更可靠。

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. Window.name仅存储一个字符串,如果您需要存储一个复杂的对象或多个值,则必须使用JSON.Stringify。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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