SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Threaded View

  1. #1
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    325
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Same page with multiple matchMedia queries - newbie question

    Hello everyone,

    I'd be very happy if someone could please help me out. I'm learning website design and JavaScript is presenting a few difficulties.

    I have the following code on my webpage:

    Code:
    <script type="text/javascript">
    if (matchMedia) {
            var mquery = window.matchMedia("(max-width:601px )");
            mquery.addListener(widthChange);
            widthChange(mquery);
    }
    function widthChange(mquery) {
            if (mquery.matches) {
    document.getElementById;
                    
            }
            else {
    document.getElementById;
    
            }
    }
    </script>
    
    <script type="text/javascript">
    if (matchMedia) {
            var mq = window.matchMedia("(max-width:484px )");
            mq.addListener(WidthChange);
            WidthChange(mq);
    }
    function WidthChange(mq) {
            if (mq.matches) {document.getElementById;
                    
            }
            else {document.getElementById;
                    
            }
    }
    </script>
    The scripts work when the if else statements are between their own opening/closing <script> tags (as per the above code), but not if I add them all to single opening/closing <script> tags. I wanted to move most of my JavaScript to an external file, so this is a bit of a dilemma. Only the first if (matchMedia) statement appears to work when I add both statements to an external JavaScript file.

    What am I missing?

    Thanks in advance!!!



    I've removed unnecessary code after document.getElementById because it has no bearing with regards to the problem at hand.
    Last edited by Pullo; Jul 4, 2013 at 02:42. Reason: Added code tags


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •