javascript - Help resolving SyntaxError: Parse error - Stack Overflow

I'm getting a SyntaxError: Parse Error when my browser runs the following code for an iPhone:if (w

I'm getting a SyntaxError: Parse Error when my browser runs the following code for an iPhone:

if (window.innerWidth && window.innerWidth <= 480) {
    $(document).ready(function(){
        $('#usernav ul').addClass('hide');
        $('#usernav').append('<div class="leftButton"
        onclick="toggleMenu()">Menu</div>');
    });
    function toggleMenu() {
        $('#usernav ul').toggleClass('hide');
        $('#usernav' .leftButton').toggleClass('pressed');
    }
}

I'm new to all of this (programming, programming languages, etc.) but I'm wondering if this error is caused because I'm viewing my site in a browser.

I've noticed that when I drag my browser's window so the viewing width decreases, my styles degrade as the width decreases. Most sites (including SO it seems) don't allow this degradation in a browser, so I guess my questions are:

  1. What's the error in my JavaScript
  2. What are the best ways to stop my browser from degrading?

Hopefully that makes sense. Maybe the degradation isn't related to the JavaScript but since it's relevant I figured I'd ask.

EDIT: I've updated the code to close off the append but I'm still getting errors at the $('#usernav').append('<div class="leftButton" line. My IDE says "unterminated string literal".

I'm getting a SyntaxError: Parse Error when my browser runs the following code for an iPhone:

if (window.innerWidth && window.innerWidth <= 480) {
    $(document).ready(function(){
        $('#usernav ul').addClass('hide');
        $('#usernav').append('<div class="leftButton"
        onclick="toggleMenu()">Menu</div>');
    });
    function toggleMenu() {
        $('#usernav ul').toggleClass('hide');
        $('#usernav' .leftButton').toggleClass('pressed');
    }
}

I'm new to all of this (programming, programming languages, etc.) but I'm wondering if this error is caused because I'm viewing my site in a browser.

I've noticed that when I drag my browser's window so the viewing width decreases, my styles degrade as the width decreases. Most sites (including SO it seems) don't allow this degradation in a browser, so I guess my questions are:

  1. What's the error in my JavaScript
  2. What are the best ways to stop my browser from degrading?

Hopefully that makes sense. Maybe the degradation isn't related to the JavaScript but since it's relevant I figured I'd ask.

EDIT: I've updated the code to close off the append but I'm still getting errors at the $('#usernav').append('<div class="leftButton" line. My IDE says "unterminated string literal".

Share Improve this question edited Sep 23, 2011 at 4:00 tvalent2 asked Sep 22, 2011 at 23:58 tvalent2tvalent2 5,00911 gold badges47 silver badges89 bronze badges 1
  • Why don't you create the element with jQuery and get rid of the inline event handler? $('<div />', {'class': "leftButton", text: 'Menu', click: toggleMenu}).appendTo('#usernav'); – Felix Kling Commented Sep 23, 2011 at 0:27
Add a ment  | 

3 Answers 3

Reset to default 2

Javascript can end lines with semicolons OR newlines. When you put in a new line, you ended your statement early. Put the statement on one whole line.

The problem is here:

$('#usernav').append('<div class="leftButton"
    onclick="toggleMenu()">Menu</div>;

You're not closing .append('. You want this:

$('#usernav').append('<div class="leftButton" onclick="toggleMenu()">Menu</div>');
$('#usernav').append('<div class="leftButton" onclick="toggleMenu()">Menu</div>;');

I guess you forgot to close the function append() with the bracket and the semicolon. :)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745457852a4628570.html

相关推荐

  • javascript - Help resolving SyntaxError: Parse error - Stack Overflow

    I'm getting a SyntaxError: Parse Error when my browser runs the following code for an iPhone:if (w

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信