1

I'm working in ASP.Net MVC and I'm trying to move all my jQuery code into .js files.

One weakness of MVC is using Razor syntax in a .JS file. So, I am doing something like this:

Html/Razor:

<input type="hidden" id="appointmentID" value="@(Model.Appointment.ID)" />

then in my .js, I do:

$(document).ready(function () {
    var appointmentID = $("#appointmentID").val();
});

My question is, will each browser instance keep it's own version of this .js file, or will a new browser tab overwrite the appointmentID variable?

5 Answers 5

3

You seem to be confused about the file itself and the execution of the file. The file is always the same, but the states it goes through depends on variables passed in (the value in your input in this case). The file itself isn't changing, only the state the code is in.

In your example:

$(document).ready(function () {
    var appointmentID = $("#appointmentID").val(); //@(Model.Appointment.ID)
});

@(Model.Appointment.ID) is just a value assigned to appointmentID in memory. That has nothing to do with what is written in the file...it's just that what is written in the file tells js what to put into memory.

Update based on your comments:

Familiarize yourself with the window variable/property. You can check it out by going anywhere in js and doing console.log(window). If you then make a global variable, you will see that it is a property of window. I recently gave a detailed explanation of this here (click). Basically, whatever you're doing in an html document is inside of window. It's pretty clear to see what a different document is. Different pages...different tabs, etc. If the page loaded - it's a different document. Obviously, iframes will be a slightly odd case, since it's a document inside of a document, but the same logic applies. Also, not everything is necessarily a "property" of anything. Just because everything is executed in the context of window doesn't mean that everything is a property of window. That is beyond the scope of your question, so I'll just say that you could study about scope/closures/encapsulation.

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

3 Comments

Right, I understand that the actual file isn't changing, but what is the scope of appointmentID? Is there 1 copy per session? Per browser? Per window? Per tab?
@Scottie I'll update my answer to explain that. It isn't very clear in your question.
You're right. My question wasn't clear. Apologies for that.
1

will each browser instance keep it's own version of this .js file

New tabs (or windows) in the same browser will typically cache the js file, so it won't be downloaded twice. If you open the page in a different browser (Chrome, Firefox, etc.) then that browser will download a fresh copy.

will a new browser tab overwrite the appointmentID variable?

Browser instances don't share javascript state. The code running in your tabs is completely separate, so changing the appointmentID in one browser tab won't affect the appointmentID in another browser tab.

Comments

1

Each browser tab lives in it's own context. So a javascript variable defined in a js file can have a different value on each opened tab. And you cannot read or write a variable from different tabs.

A tab works as a new browser instance, only cookies has I know are shared between tabs.

Comments

0

To clear understanding the browsers are able to use cached copy of .js file however each page/tab may not download .js file again however all of them refer to their own page and does not link any way with overriding variables of other page.

Comments

0

If you will open this tabs in different time when you Model will have different Id I think they will have different value,so the old value will not be overloaded without page refresh. If you area worried for some kind of validation just do server side and don't worry about it.

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.