最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - is it possible to change JS variable value from browser console? - Stack Overflow

programmeradmin3浏览0评论

If I have in main.js's file, global variable a = 5; and I'm sending this variable with AJAX to PHP, is it possible to change this variable from the console or somehow from the outside and have AJAX send wrong parameter?

Here is an example:

var init = {
    id: null,
    setId: function(i){
        this.id = i;
        alert(this.id);
    },
    callAjax: function(){
        alert(this.id);
    }
};

If I have this, is it still possible to change?

If I have in main.js's file, global variable a = 5; and I'm sending this variable with AJAX to PHP, is it possible to change this variable from the console or somehow from the outside and have AJAX send wrong parameter?

Here is an example:

var init = {
    id: null,
    setId: function(i){
        this.id = i;
        alert(this.id);
    },
    callAjax: function(){
        alert(this.id);
    }
};

If I have this, is it still possible to change?

Share Improve this question edited Oct 12, 2014 at 19:36 rink.attendant.6 46.4k64 gold badges110 silver badges157 bronze badges asked Oct 12, 2014 at 18:45 PiraTaPiraTa 4852 gold badges8 silver badges22 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

If it is accessible from console (since you are stating that its a global variable), it is possible to modify it from console, just by writing a=1 or anything.

If this variable is global than it is possible.

To prevent it you must use closures

Yes, you can open the browser's console (For Chrome: Ctrl+ Shift + I on PC, Command + Shift + I on Mac) and switch to the 'Console' tab. Then just edit the global variable by setting a=<whatever you want>.

Because it is in the global scope, you will be able to change it before the AJAX request.

发布评论

评论列表(0)

  1. 暂无评论