1

I have a small piece of code for a template project I'm working on. There are three separate buttons, that point to three separate locations. In order to make it easier for content providers, I have these buttons calling minimal routines to load the next page.

The code is as follows:

    /* navigation functions here for clarity and ease of editing if needed */
    prevURL    = 'ch0-2.html';
    nextURL    = 'ch2-1.html';
    manURL     = 'ch1-2.html';

    function prevPage() {
       window.location = prevURL; 
    }

    function nextPage() {
        window.location = nextURL;
    }

    function goManager() {
        window.location = manURL;
    }

This works perfectly in Firefox and Chrome, but seems to fail in Internet Explorer.

I open up the developer tools in IE (F12) and am presented with the message:

SCRIPT5009: 'manURL' is undefined

The location information (line 43, character 13) points to the "window.location = manURL" part of the code.

However, once the developer tools are open, if I hit F5 to reload the page, the button works without error until I close IE and reopen it, where it once again fails to respond and gives the same "undefined" error.

I'm baffled. Anyone have any ideas?

UPDATE I know the variable declaration is poor, and that I can use window.location.href instead. What is relevant here is that the other two pieces of code, which are identical in all of these significant ways, work perfectly either way.

epascarello has put me on the right track. by removing all console.log commands, everything starts working. I'm just wondering why this happens, and would like to be able to give epascarello credit for helping me.

3 Answers3

2

IE does not have console commands when the developer window is not open. So if you have them in there the code will not run. It will error out.

You can either comment out the lines or add in some code that adds what is missing.

if (typeof console === "undefined") {
   console = {
       log : function(){},
       info : function(){},
       error : function(){}
       //add any others you are using
   }
}
epascarello
  • 204,599
  • 20
  • 195
  • 236
1

Try setting

window.location.href

instead of just window.location.

Source: http://www.webdeveloper.com/forum/showthread.php?105181-Difference-between-window.location-and-window.location.href

Everett Green
  • 632
  • 3
  • 8
0

Always define variables before using them ,being explicit (expressing the intention) is a good practice,

so define your variables like this,

var prevURL    = 'http://google.com';
var nextURL    = 'http://msn.com';
var manURL     = 'http://stackoverflow.com';

You can try using

window.location.href

refer to this post for difference

Suggestion: Make a jsfiddle.net for us so we could guide you easily

Community
  • 1
  • 1
DayTimeCoder
  • 4,294
  • 5
  • 38
  • 61