JavaScript not running in Internet Explorer

2020-07-21 05:49发布

问题:

I wrote some JavaScript to move an ad banner when the user is scrolling. However, it's working fine with every browser except Internet Explorer... It seems like my JS is not even starting... I'm using the latest IE9.

How I'm invoking it:

<html>
<head>
  <script src="./js/move-it.js" type="text/javascript"></script>
</head>
<body>
  ...
  <div id="bird">
    <iframe ...>...</iframe>
  </div>
  <script type="text/javascript">
    start(); // method in move-it.js
  </script>
</body>

Website: http://lolkitten.org

Btw, can anyone tell me how to prevent the banner from crashing into my footer in a nice way? I tried to put a div as "stopper" above the lower ad and use it's "offset-top" attribute, but I guess it always gave me a too small value, i.e. it kept crashing... -.-

Cheers

回答1:

I managed to get it to work now. The problem was that IE does not allow const modifiers in JavaScript. I simply changed them to var and it worked finely.



回答2:

IE, all versions is not tolerant of common errors made by PHP programmers. Here is a list of Javascript syntax not accepted by IE, for all functions, including AJAX:

(These IE Javascript syntax errors are not errors in Opera, Chrome and Firefox.)

  1. you cannot set a default variable value in function declaration parameters function thisFunction(something='x'){ thingy.here;} is not allowed and will read as an undeclared function when thisFunction() is called.

  2. Passing objects as function parameters can have unexpected results: function(someObject) may or may not work depending on the context.

  3. undeclared variables stop the script

  4. event.preventDefault(); cannot be called inside the called function, and will stop the script

  5. event.preventDefault(); must be declared first, in the event reference before all other functions... this is not true on other browsers. So IE must operate asynchronously by default...

    May not be expected when PHP programmers first learn to enjoy the synchronous character of Javascript as lesson #1 in the language. This example does not work (does not work) in the 'a' tag when put directly into the tag of a link

    onclick="function() { 
        if(typeof someFunction === 'function') { 
            event.preventDefault(); 
            someFunction('anyParameter'); 
        };"
    

    When you are using the same code on a page that doesn't implement or declare the function someFunction(). what a waste of time!

  6. onclick='clickChild(this);' doesn't work on IE... and I don't know why. It will actually stop a contained link and nothing will happen. Seems to contradict the above behavior of preventDefault which only works as the first function in an event reference, and if not called first a containing link will be followed. So inconsistent logic in IE. ---as of today, January 21, 2017, in today's Google Chrome update, the above function also does not work in Chrome.

In forms, the 'button' tag does not return the value correctly for form submission. This may be fixed in new IE versions,or it may not be. For my current project: yad1.org the button tag is required for multilingual submission button names that require the same value for all languages.

Conclusion: IE should be removed from Windows and forgotten forever.

Real waste of time debugging. IE needs to get in touch with the programmer friendly world of Javascript.



回答3:

  1. On the Tools menu, click Internet Options, and then click the Security tab.
  2. Click the Internet zone.
  3. If you do not have to customize your Internet security settings, click Default Level. Then do step 4
    If you have to customize your Internet security settings, follow these steps:

    a. Click Custom Level.
    b. In the Security Settings – Internet Zone dialog box, click Enable for Active Scripting in the Scripting section.

  4. Click the Back button to return to the previous page, and then click the Refresh button to run scripts.



回答4:

A fairly long time later ... same issue on a customer machine with IE11 (Firefox & Edge working fine). The file at (UNC)

file://///server/share/directory/index.html

was loaded correctly, but javascript is not activated.

The solution in this case:

  1. "Internet Options" > Tab "Security" > Zone "Local intranet" > Button "Sites" > Deactivate the third option "Include all network paths (UNCs)" (page reload needed)
  2. "Internet Options" > Tab "Security" > Zone "Trusted sites" > Button "Sites" > Add "file://server" to the Websites (IE restart needed)

Zone Local intranet

Now it works.