0

i have found this error like :

Uncaught SyntaxError: Unexpected token } 

in chrome how ever all other browser like Mozilla and IE doesn't give me this error.

and this is my script:

<script type="text/javascript" language="javascript">
$(document).ready(function(){                 
     $(".thumb").click(function(){
     window.location=$(this).find("a").attr("href");return false;
     });
});
</script>

----------------------------Updated---------------------------------------------

i have also another script:

<script type="text/javascript">

        $(document).ready(function() {

            // Variables
            var objMain = $('#main');

            // Show sidebar
            function showSidebar() {
                objMain.addClass('use-sidebar');
                $.cookie('sidebar-pref2', 'use-sidebar', { expires: 30 });
            }

            // Hide sidebar
            function hideSidebar() {
                objMain.removeClass('use-sidebar');
                $.cookie('sidebar-pref2', null, { expires: 30 });
            }

            // Sidebar separator
            var objSeparator = $('#separator');

            objSeparator.click(function(e) {
                e.preventDefault();
                if (objMain.hasClass('use-sidebar')) {
                    hideSidebar();
                }
                else {
                    showSidebar();
                }
            }).css('height', objSeparator.parent().outerHeight() + 'px');

            // Load preference
            if ($.cookie('sidebar-pref2') == null) {
                objMain.removeClass('use-sidebar');
            }
        });
    </script>
Jack Bonneman
  • 1,821
  • 18
  • 24
shalin gajjar
  • 646
  • 4
  • 15
  • 44
  • 7
    This works fine > http://jsfiddle.net/MvvTU/ The problem must be with another snippet of your code. – BenM Jan 22 '14 at 11:02
  • As @BenM said, it is correct. Try to make a pastebin or fiddle of your code, maybe we can help in better way. – steo Jan 22 '14 at 11:03
  • @BenM - but chrome only show this portion has defect. – shalin gajjar Jan 22 '14 at 11:08
  • This error generally means that you've got an unclosed bracket somewhere, which makes JavaScript expect that closing bracket, instead of a `}`. – Joeytje50 Jan 22 '14 at 11:11
  • 1
    This code doesnt throw any error. The error is elsewhere. Chrome might show error in this part but you might added have opened/closed curly brackets at a wrong place elsewhere due to which chrome is unable to parse this part of code. – shinobi Jan 22 '14 at 11:13
  • @BenM - when page load time this error occurred every time and how ever after i click something like link button of that master page then it's normal. – shalin gajjar Jan 22 '14 at 11:13
  • @BenM - ya i put other script in updated section that in master page portion. check it. – shalin gajjar Jan 22 '14 at 11:18
  • @shal nothing wrong with the updated code – Anton Jan 22 '14 at 11:18

2 Answers2

2

Use window.location.href instead of window.location as window.location is an object like,

window.location.href=$(this).find("a").attr("href");

Code

$(document).ready(function(){                 
   $(".thumb").click(function(){
      window.location.href=$(this).find("a").attr("href");
      return false;
   });
});

Read window.location Does Not Work on Chrome Browser

Updated, Chrome doesn't support cookies for locals unless you start it with the --enable-file-cookies flag.

Read Javascript fails to create/recognize cookies on Chrome

Community
  • 1
  • 1
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • 1
    This is an outdated issue - assigning a string to `window.location` is just an alias of `window.location.href` and works perfectly fine in chrome. – Emissary Jan 22 '14 at 11:13
  • @shal try my updated answer. – Rohan Kumar Jan 22 '14 at 11:25
  • @Emissary, it may be outdated for Chrome, but as of today, Brave throws the same error. – Sablefoste Aug 22 '17 at 15:56
  • @Sablefoste I just downloaded the latest stable version and it seems fine - in any case the expected behaviour is written into the [JS spec](https://www.w3.org/TR/html5/browsers.html#the-location-interface), if there's a bug in a browser it should be reported as such. – Emissary Aug 22 '17 at 17:37
0

Please try this

   <script type="text/javascript" language="javascript">
    $(document).ready(function(){                 
         $(".thumb").click(function(){
         window.location.href=$(this).find("a").attr("href");
         });
    });
    </script>
Systematix Infotech
  • 2,345
  • 1
  • 14
  • 31
  • there is a difference. window.location is a Location object. window.location.href is a string representation of the location. – Systematix Infotech Jan 22 '14 at 11:34
  • Yes, but assigning a string to `window.location` should work (it acts as an alias to the `.href`). Anyway, my point is that I don't think that it could explain the error about the `}`. – nnnnnn Jan 22 '14 at 12:26