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

jquery - Math.random() javascript function *undefined* on Chrome - Stack Overflow

programmeradmin13浏览0评论

In first place, I am convinced that this is a trivial question, but I cannot understand why this is happening and I couldn't find an answer anywhere else. I googled my issue with little success, but if I missed something and just wasted your time please point me in the right direction and accept my humble excuses.

That said, here is what happens. I am building a simple script to display a graph drawn by a distributed algorithm (giraph) and I am working on Linux. I import all the libraries and, in particular, jQuery and everything works in Firefox (version 36.0.1). Switching to Chrome (version 41.0.2272.89 (64-bit)), the page stops working. Investigating the issue, I found out that the error was inside the jQuery.extend() function into the jQuery library, at the following line:

expando: "jQuery" + ( version + Math.random() ).replace( /\D/g, "" ),

In fact, trying to invoke Math.random() into the browser console leads to:

> Math.random();
> Uncaught TypeError: Undefined is not a function

Anyway, while typing the console autocompletes the "Math" variable, with the following result:

> Math
> function (){return "";}

This does not happen with Chrome on MacOS X Mavericks. Did anybody experience this kind of behaviour before?

EDIT: Unfortunately, I don't think that this is a namespace conflict. In my library I import only one other library (other than jQuery), which is Sigma js. As suggested, I wrote Math in the console and tried to understand which library did override Math (using "Show Function Definition"), with the following result, taken from a file named "VM53" (which I did not write and/or linked directly).

(function (){
    for (var i in window)
    {
    try {
            var jsType = typeof window[i];
            switch (jsType.toUpperCase())
            {                   
                case "FUNCTION": 
                    if (window[i] !== window.location)
                    {
                        if (window[i] === window.open || (window.showModelessDialog && window[i] === window.showModelessDialog))
                            window[i] = function(){return true;};
                        else if (window[i] === window.onbeforeunload)   // To try to fix onbeforeunload pop ups some users report seeing but I can't replicate.
                            window.onbeforeunload = null;
                        else if (window[i] === window.onunload)
                            window.onunload = null;                             
                        else
                            window[i] = function(){return "";};
                    }
                    break;                          
            }           
        }
        catch(err)
        {}      
    }

    for (var i in document)
    {
        try {
            var jsType = typeof document[i];
            switch (jsType.toUpperCase())
            {                   
                case "FUNCTION":
                    document[i] = function(){return "";};
                    break;                  
            }           
        }
        catch(err)
        {}      
    }

    try {
        eval = function(){return "";};              
        unescape = function(){return "";};
        String = function(){return "";};
        parseInt = function(){return "";};
        parseFloat = function(){return "";};
        Number = function(){return "";};
        isNaN = function(){return "";};
        isFinite = function(){return "";};
        escape = function(){return "";};
        encodeURIComponent = function(){return "";};
        encodeURI = function(){return "";};
        decodeURIComponent = function(){return "";};
        decodeURI = function(){return "";};
        Array = function(){return "";};
        Boolean = function(){return "";};
        Date = function(){return "";};
        Math = function(){return "";};
        Number = function(){return "";};
        RegExp = function(){return "";};

        var oNav = navigator;
        navigator = function(){return "";};
        oNav = null;            
    }
    catch(err)
    {}

})();

In first place, I am convinced that this is a trivial question, but I cannot understand why this is happening and I couldn't find an answer anywhere else. I googled my issue with little success, but if I missed something and just wasted your time please point me in the right direction and accept my humble excuses.

That said, here is what happens. I am building a simple script to display a graph drawn by a distributed algorithm (giraph) and I am working on Linux. I import all the libraries and, in particular, jQuery and everything works in Firefox (version 36.0.1). Switching to Chrome (version 41.0.2272.89 (64-bit)), the page stops working. Investigating the issue, I found out that the error was inside the jQuery.extend() function into the jQuery library, at the following line:

expando: "jQuery" + ( version + Math.random() ).replace( /\D/g, "" ),

In fact, trying to invoke Math.random() into the browser console leads to:

> Math.random();
> Uncaught TypeError: Undefined is not a function

Anyway, while typing the console autocompletes the "Math" variable, with the following result:

> Math
> function (){return "";}

This does not happen with Chrome on MacOS X Mavericks. Did anybody experience this kind of behaviour before?

EDIT: Unfortunately, I don't think that this is a namespace conflict. In my library I import only one other library (other than jQuery), which is Sigma js. As suggested, I wrote Math in the console and tried to understand which library did override Math (using "Show Function Definition"), with the following result, taken from a file named "VM53" (which I did not write and/or linked directly).

(function (){
    for (var i in window)
    {
    try {
            var jsType = typeof window[i];
            switch (jsType.toUpperCase())
            {                   
                case "FUNCTION": 
                    if (window[i] !== window.location)
                    {
                        if (window[i] === window.open || (window.showModelessDialog && window[i] === window.showModelessDialog))
                            window[i] = function(){return true;};
                        else if (window[i] === window.onbeforeunload)   // To try to fix onbeforeunload pop ups some users report seeing but I can't replicate.
                            window.onbeforeunload = null;
                        else if (window[i] === window.onunload)
                            window.onunload = null;                             
                        else
                            window[i] = function(){return "";};
                    }
                    break;                          
            }           
        }
        catch(err)
        {}      
    }

    for (var i in document)
    {
        try {
            var jsType = typeof document[i];
            switch (jsType.toUpperCase())
            {                   
                case "FUNCTION":
                    document[i] = function(){return "";};
                    break;                  
            }           
        }
        catch(err)
        {}      
    }

    try {
        eval = function(){return "";};              
        unescape = function(){return "";};
        String = function(){return "";};
        parseInt = function(){return "";};
        parseFloat = function(){return "";};
        Number = function(){return "";};
        isNaN = function(){return "";};
        isFinite = function(){return "";};
        escape = function(){return "";};
        encodeURIComponent = function(){return "";};
        encodeURI = function(){return "";};
        decodeURIComponent = function(){return "";};
        decodeURI = function(){return "";};
        Array = function(){return "";};
        Boolean = function(){return "";};
        Date = function(){return "";};
        Math = function(){return "";};
        Number = function(){return "";};
        RegExp = function(){return "";};

        var oNav = navigator;
        navigator = function(){return "";};
        oNav = null;            
    }
    catch(err)
    {}

})();
Share Improve this question edited Nov 15, 2016 at 19:47 Alexis Wilke 20.7k11 gold badges104 silver badges177 bronze badges asked Mar 16, 2015 at 8:51 Alessio ArleoAlessio Arleo 711 gold badge1 silver badge6 bronze badges 4
  • That is indeed interesting :) – Piotr Dajlido Commented Mar 16, 2015 at 8:54
  • 5 Some script overrided Math.random. Try to remove other <script> tags from page and see what causes error. – Ginden Commented Mar 16, 2015 at 8:55
  • Or a Chrome extension disables Math for some reason. Does it work if you try it in incognito mode? – JJJ Commented Mar 16, 2015 at 8:57
  • Math.random() is native JavaScript code, so the only thing that comes to my mind is that somewhere in your code you have overwritten Math object. – Roumelis George Commented Mar 16, 2015 at 8:59
Add a comment  | 

4 Answers 4

Reset to default 11

Something overrides Math in your code.

Execute Math in your chrome console. Execute context menu on it and select "Show function definition". This will probably lead you to "Sources" panel and show script where it is being overriden.

EDIT: this is chrome extension called notscripts. Disable it. Here is the source code for proof: webarchive

Another library on your page is overriding the Math object. This is one of the reasons that polluting the Global Namespace is frowned upon.

For reference Math should return the following

I found another situation that could cause this to happen. I was converting a PHP function to a JS function and I had the following line:

ticks += "," . Math.floor(yyy * intMax);

Changing it to

ticks += "," + Math.floor(yyy * intMax);

solved the problem

Somehow, NotScript got automatically installed in my Chrome browser on Ubuntu 16.04. That inserts the script you show in your update just after the <html> tag.

I had to go to the settings, look at the extensions, disable the NotScripts and then restart Chrome to it would be gone.

On Ubuntu, this must be very recent or the script got updated recently because I ran the exact same test in Chrome, with the exact same scripts, a few days ago, and it was working just fine.

Interestingly enough, it looks like it was removed 2 years ago (Nov 2014) and maybe re-added recently?

发布评论

评论列表(0)

  1. 暂无评论