Trouble adding jquery

Started by TwitchisMental, July 15, 2012, 11:11:49 PM

Previous topic - Next topic

TwitchisMental

So I am trying to add a simple jquery script to a theme I am working on. Problem is I keep getting a php error.


It is probably due to the ' marks used in the jquery.


Here is the code giving an issue -




<script>


$(function () {
 
  var msie6 = $.browser == 'msie' && $.browser.version < 7;
 
  if (!msie6) {
    var top = $('# main_menu ').offset().top - parseFloat($('# main_menu').css('margin-top').replace(/auto/, 0));
    $(window).scroll(function (event) {
      // what the y position of the scroll is
      var y = $(this).scrollTop();
     
      // whether that's below the form
      if (y >= top) {
        // if so, ad the fixed class
        $('#main_menu').addClass('fixed');
      } else {
        // otherwise remove it
        $('# main_menu ').removeClass('fixed');
      }
    });
  } 
});
</script>



If you need any more information just ask :).

mashby

What is the php error? If it's indeed the ', just escape it, or use " instead. And instead of just <script>, it should be:
<script type="text/javascript">
Always be a little kinder than necessary.
- James M. Barrie

TwitchisMental

Quote from: mashby on July 15, 2012, 11:13:53 PM
What is the php error? If it's indeed the ', just escape it, or use " instead. And instead of just <script>, it should be:
<script type="text/javascript">


The error is -


Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in .../Themes/****/index.template.php on line 141

mashby

Always be a little kinder than necessary.
- James M. Barrie

TwitchisMental

Quote from: mashby on July 15, 2012, 11:25:07 PM
And what's line 141?


  var msie6 = $.browser == 'msie' && $.browser.version < 7;

mashby

Wondering what happens if you make it this instead?
  var msie6 = $.browser == \'msie\' && $.browser.version < 7;
Always be a little kinder than necessary.
- James M. Barrie

TwitchisMental

Quote from: mashby on July 15, 2012, 11:31:27 PM
Wondering what happens if you make it this instead?
  var msie6 = $.browser == \'msie\' && $.browser.version < 7;
I did that and now I get this error (Oh this is fun lol)


Parse error: syntax error, unexpected '$', expecting ',' or ';' in .../Themes/*****/index.template.php on line 145

The code on line 145 -



$(window).scroll(function (event) {

mashby

How about taking the entire block of code and making it this? :)
<script type="text/javascript">
$(function () {
 
  var msie6 = $.browser == \'msie\' && $.browser.version < 7;
 
  if (!msie6) {
    var top = $(\'#main_menu\').offset().top - parseFloat($(\'#main_menu\').css(\'margin-top\').replace(/auto/, 0));
    $(window).scroll(function (event) {
      // what the y position of the scroll is
      var y = $(this).scrollTop();
     
      // whether that\'s below the form
      if (y >= top) {
        // if so, ad the fixed class
        $(\'#main_menu\').addClass(\'fixed\');
      } else {
        // otherwise remove it
        $(\'#main_menu\').removeClass(\'fixed\');
      }
    });
  } 
});
</script>
Alternatively, all of the \' could just be "
Always be a little kinder than necessary.
- James M. Barrie

TwitchisMental

Quote from: mashby on July 15, 2012, 11:38:02 PM
How about taking the entire block of code and making it this? :)
<script type="text/javascript">
$(function () {
 
  var msie6 = $.browser == \'msie\' && $.browser.version < 7;
 
  if (!msie6) {
    var top = $(\'#main_menu\').offset().top - parseFloat($(\'#main_menu\').css(\'margin-top\').replace(/auto/, 0));
    $(window).scroll(function (event) {
      // what the y position of the scroll is
      var y = $(this).scrollTop();
     
      // whether that\'s below the form
      if (y >= top) {
        // if so, ad the fixed class
        $(\'#main_menu\').addClass(\'fixed\');
      } else {
        // otherwise remove it
        $(\'#main_menu\').removeClass(\'fixed\');
      }
    });
  } 
});
</script>
Alternatively, all of the \' could just be "


THANK YOU that got it working :D.

mashby

Cool. Love jQuery. Look forward to seeing your final product! :)
Always be a little kinder than necessary.
- James M. Barrie

TwitchisMental

Quote from: mashby on July 15, 2012, 11:40:39 PM
Cool. Love jQuery. Look forward to seeing your final product! :)
I love Jquery too :). Thanks again.

Advertisement: