-1

This question is related to my previous question : How To Set Value In <style> ... </style> According To Browser Type Got From Javascript?

Someone suggested I use "CSS Vendor prefixes", so I tried, but it didn't work with IE, and my problem is with IE, you can see it from my site : http://gatecybertech.com/

On my site, the center image shifts a few pixels in IE when you mouse over it, but it works fine with other browsers.

So I did some research and found a piece of code that shows me how to reach the properties in CSS from JavaScript and set a new value, and I've also found some code to detect browser, so if I combine them I can first detect if it's IE, if so, then I can reach the property I want and adjust its value.

But the problem I have now is how to reach a complex property, like the one below in my sample code : .pic-container-1 .pic-hover -> left

<!DOCTYPE html>
<html>
<head>
  <title>Changing style</title>
  <meta charset="utf-8" />
  <style>
    #elem
    {
      width: 200px; background-color: lime;
    }
    .pic-container-1{display:block; position:relative; }
    .pic-container-1 .pic-box{display:block;}
    .pic-container-1 .pic-box img{display:block;}
    .pic-container-1 .pic-hover{position:absolute; top:0px; left:866px; display:none;}
    .pic-container-1:hover .pic-hover{display:block;}
  </style>

  <script type="text/javascript">
    function getStyle(elem, cssprop, cssprop2)
    {
      if (elem.currentStyle)                                                   // IE
      {
        return elem.currentStyle[cssprop];
      }
      else if (document.defaultView && document.defaultView.getComputedStyle)  // other browsers
      {
        return document.defaultView.getComputedStyle(elem, null).getPropertyValue(cssprop2);
      }
      else                                                                     // fallback
      {
        return null;
      }
    }

    window.onload = function ()
    {
      /*                                                                        // Code demo how to access CSS properties
       var elem=document.getElementById("elem");                                // setting and accessing style properties

       var color=getStyle(elem,"backgroundColor","background-color");
       alert(color);                                                            // rgb(0,255,0)

       elem.style.width="500px";
       elem.style.backgroundColor="yellow";

       alert(elem.style.width);                                                 // 500px
       alert(elem.style.backgroundColor);                                       // yellow

       elem.style["fontFamily"]="Courier";                                      // array notation

       var style=elem.getAttribute("style");                                    // demonstrating overwriting properties
       alert(style);                                                            // should display color: purple; width: 500px;
       // background-color: yellow;

       elem.setAttribute("style","height: 100px");
       var style=elem.getAttribute("style");
       alert(style);                                                            // now only displays height,resets styles

       var font=getStyle(elem,"fontFamily","font-family");
       alert(font);                                                             // default font family
       */

        // Opera 8.0+
        var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;

        // Firefox 1.0+
        var isFirefox = typeof InstallTrigger !== 'undefined';

        // Safari 3.0+ "[object HTMLElementConstructor]" 
        var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || safari.pushNotification);

        // Internet Explorer 6-11
        var isIE = /*@cc_on!@*/false || !!document.documentMode;

        // Edge 20+
        var isEdge = !isIE && !!window.StyleMedia;

        // Chrome 1+
        var isChrome = !!window.chrome && !!window.chrome.webstore;

        // Blink engine detection
        var isBlink = (isChrome || isOpera) && !!window.CSS;

        var output = 'Detecting browsers by ducktyping :\n===========================\n';
        output+='isChrome: '+isChrome+'\n';      // 57.8 % Market Share
        output+='isSafari: '+isSafari+'\n';      // 14.0 %
        output+='isFirefox: '+isFirefox+'\n';    // 6.0 %
        output+='isIE: '+isIE+'\n';
        output+='isEdge: '+isEdge+'\n';          // 5.9 %  IE + Edge
        output+='isOpera: '+isOpera+'\n';        // 3.7 %
        output+='isBlink: '+isBlink+'\n';

//        alert(output);                                                       // Code demo how to detect browser

        if (isIE) 
        {
          // code here to adjust for IE
          // The images seem to work correctly [ overlapping each other when you mouse over them ] in this test html, but on my site I need to adjust a few pixels for IE
        }

//    .pic-container-1 .pic-hover{position:absolute; top:0px; left:42px; display:none;}

      var a = document.getElementById("pic-container-1");
//      var a = document.getElementById("pic-container-1.pic-hover");          // Doesn't work. Trying to get to prpperty : .pic-container-1 .pic-hover -> left
      alert(a.style);                                                          // How to reach and set the value of : " .pic-container-1 .pic-hover -> left " ?

    }
  </script>
</head>
<body>
  <div id="elem" style="color: purple">
    testing
  </div>

  <div id="localizejs">
    <div class="content">
      <div class="main">
        <div class="container center">

          <center>
            <a href=http://gatecybertech.net>
              <div id=pic-container-1 class="pic-container-1">
                <div class="pic-box"><img src="http://gatecybertech.com/GATE_Frame_1.PNG" alt="GATE"></div>
                <div class="pic-box pic-hover"><img src="http://gatecybertech.com/GATE_Frame_2.PNG" alt="GATE"></div>
              </div>
            </a>
          <center>

        </div>
      </div>
    </div>
  </div>

</body>
</html>

So how to set the value at 600 [ for example ] for IE and 800 for all other browsers ?

Frank
  • 30,590
  • 58
  • 161
  • 244
  • The main way to check is to use the user agent string which is not always reliable. You could check for certain features. https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser/9851769 I would not use a site for cybersecurity that does not at least use HTTPS when there are even free options to get SSL certs. – Daniel Gale Aug 30 '18 at 17:44
  • 2
    *I've also found some code to detect browser,* There is no way to reliably check the browser type - - it's ultimately a wild goose chase. `.getComputedStyle()` [works in every modern browser](https://caniuse.com/#search=getComputedStyle) and has for some time. Don't do browser detection, do [feature detection](https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Cross_browser_testing/Feature_detection). – Scott Marcus Aug 30 '18 at 17:47
  • This sounds like an [XY Problem](http://xyproblem.info/). If you follow modern standards, use a reset or normalize CSS file, and stay away from vendor-specific APIs, the page will render the same in all browsers and none of this would be necessary. – Scott Marcus Aug 30 '18 at 17:53
  • 1
    Note: that the `
    ` has been deprecated. At a glance, this might be related to IE's handling of `center` and/or the box model offset quirks https://stackoverflow.com/questions/9264767/internet-explorer-box-model-what-is-offset. You might also want to rethink using a table for layout/positioning, which might compond the issue.
    – dperish Aug 30 '18 at 18:03
  • 1
    @dperish LOL .... done :) .. deleted prev. comment and upvoted yours – Asons Aug 30 '18 at 18:08

1 Answers1

1

You can use the style property to access the styles of the selected element (there's multiple other ways check: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style for more information). Also you can't chain selectors with document.getElementById, you can definitely do that with jQuery though.

var b = document.getElementsByClassName("pic-hover");
b[0].style.left = "800px";
Velimir Tchatchevsky
  • 2,812
  • 1
  • 16
  • 21
  • 1
    *You can use the style property to access the styles of the selected element* for setting styles, yes. For getting styles, only inline styles, which we want to avoid as much as we can. – Scott Marcus Aug 30 '18 at 17:49
  • @Daniel Gate, an honest mistake I've edited it, thanks. @ Scott Marcus, I do agree that avoiding inline styles should be the norm, but in the OPs specific case I really don't see a problem with that (correct me if I'm wrong) – Velimir Tchatchevsky Aug 30 '18 at 17:56
  • 1
    It's a big problem for two reasons 1) working with inline styles leads to much more code needing to be written and duplication of code and 2) inline styles can only be overridden with an `!important` declaration, which is universally known to be an anti-pattern. So, inline styles don't scale well. Instead, CSS classes should be set up in advanced and then dynamically added/removed as necessary. Sure, there will sometimes be a need for an inline style to override others, but it should be the exception, not the norm. – Scott Marcus Aug 30 '18 at 17:59
  • @ScottMarcus That's exactly what I think too, but at the same time OP's situations is such that he's very very unlikely to want to overwrite that particular style, a class could be reused of course in other files, but the js function containing the inline style could be reused too. – Velimir Tchatchevsky Aug 30 '18 at 18:07
  • Thanks, this is the one that solved my problem ^_^ ! – Frank Aug 30 '18 at 20:01