0

im very new in javascript and im trying to implement new function into existing project. The background - In html file i can see:

<script src="js/users.js" type="text/javascript"></script>
<script src="js/seating-plan.js"></script>

File seating-plan.js access object stored in users.js that contains:

var users = [
    {"id": 1, "name":"first", "surname":"surname"},
    {"id": 2, "name":"second", "surname":"surname"}
]

My question is: Can I change a property loaded from such object so the file will be changed? If so how to do it? I read some posts how to change object property but such approach will only changed already loaded object, not rewrite js file so i would be stuck with same result after refresh. Im guessing I will need to change how I initially load array object but if you know how to do it with my original aproach or how to do the second option in easy way please assist.
EDIT: localhost internal purpose (shared folder)

The approach I mentioned:

// Start of jQuery ready function
$(function () {

    //can already access object 
    for (var i = 0; i < users.length; i++) {

        //example of how i tried to rewrite property
        if (users[i].id === 1){
            users[i].name = "NewName";
        }
    }
    .
    .
    .
0

1 Answer 1

1

If you want to change data on the server, then you must send an instruction to the server to change the data.

It would be a serious security risk if, by default, any web browser could change data on any HTTP server. The Google homepage would be vandalised on a second-by-second basis if that were possible!

Typically this will be a POST request (which you could make with a <form> or with XMLHttpRequest/fetch).

You then need server-side code (written in the language of your choice) which will update the data.

Typically you will want to store the data in a database and generate users.js (although changing it to JSON would probably be a better idea) on demand.

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

2 Comments

Thanks for reply, i forgot to mention its localhost only - made an edit. Yeah i have seen that JSON is common loading object type, thank you!
@MartinVoroňák — That doesn't make a difference. Standard security rules and webserver capabilities are unchanged.

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.