Forum Discussion

googleid_102416's avatar
googleid_102416
Contributor
12 years ago

Changing Browser Mode of Internet Explorer


What is the best way to change Internet Explorer Browser Mode? I've posted my solution below but I don't like it because:

1) It will not work if you call switchIEBrowserMode while Developer Tool is already opened.

2) It seems to be locked to English locale.

3) page.Keys() posts warning when several tabs are opened because it sends key presses to background page.

4) Detection of toolbar is terrible.



Any ideas how to do this better?





function switchIEBrowserMode(browser_mode_id) {


var menu_items = {


"ie7": "Internet Explorer 7",


"ie8": "Internet Explorer 8",


"ie9": "Internet Explorer 9"


};


 


if(!browser_mode_id in menu_items)


return -1;


 


var browser = Sys.Browser("iexplore");


var page = browser.Page("*");


page.Keys("[F12]");


var toolbar = browser.BrowserWindow(0).Window("Frame Tab", "", 1).Tab("*").Window("BaseBar", "Horizontal Explorer Bar", 1).Window("ReBarWindow32", "", 1).Window("IEDEVTOOLS").Window("IEDEVTOOLSMENU").Window("ToolbarWindow32");


toolbar.ClickItem("&Browser Mode:*");


toolbar.PopupMenu.Click(menu_items[browser_mode_id]);


page.Keys("[F12]");


}


 


function switchIEBrowserMode_UnitTest(){


switchIEBrowserMode("ie8");


}

  • You can do it all with keystrokes (browser and document mode):




    function IE9BrowserMode(versionNumber) {


        Aliases.iexplore.Page("*").Keys("[F12]~b" + versionNumber + "~m" + versionNumber + "~[F4]");


    }

     


    function TESTIE9BrowserMode() {


        IE9BrowserMode(8);


    }



    Note you don't need to sanitise the versionNumber as any wrong keystrokes will just be ignored.



    The keystroke for IE10 will be "0" (zero). Let's hope there is no IE20.





     


  • Can somebody please test this function on different systems and report back if it's working? I guess Window("ToolbarWindow32") will fail on x64 systems.



    function switchIEBrowserMode(browser_mode_id) {


    var menu_items = {


    "ie7": "Internet Explorer 7",


    "ie8": "Internet Explorer 8",


    "ie9": "Internet Explorer 9"


    };


     


    if(!browser_mode_id in menu_items)


    return -1;


     


    var browser = Sys.Browser("iexplore");


    var browser_window = browser.BrowserWindow(0);


     


    var toolbar = findIEDevToolsMenu(browser_window);


    if(!toolbar || !toolbar.Visible) {


    browser_window.Keys("[F12]");


    toolbar = findIEDevToolsMenu(browser_window);


    }


     


    toolbar.ClickItem(9); // click on "&Browser Mode:*" menu item


    toolbar.PopupMenu.Click(menu_items[browser_mode_id]);


    browser_window.Keys("[F12]");


     


    function findIEDevToolsMenu(ieWindow) {


    var devToolsMenu = ieWindow.Find("WndClass", "IEDEVTOOLSMENU", 1000);


    if(!devToolsMenu || !devToolsMenu.Exists)


    return false;


    return devToolsMenu.Window("ToolbarWindow32");


    }


    }


     


    function switchIEBrowserMode_UnitTest(){


    switchIEBrowserMode("ie8");


    }

  • I haven't tried yet, but this does not seem to work on non-English Windows and if you call the function when F12 menu is already opened. Am I right?