Trying to set jquery to fire once after resizing










2















Is there a way to fire jquery only once when resizing? I have here a script that adds a hash(#) within the href of an anchor <a> element. The problem is the script keeps when resizing and so it keeps adding hash within the href. Is there a way to make it fire only once?



(function($)
'use strict';



var mobileMenuDrawer =

init : function()

$('.region-primary-menu').addClass('primary-mobile-menu');
$('.primary-mobile-menu .menu.-primary > .menu-item').addClass('mobile-menu-item');
$('.primary-mobile-menu .menu.-primary > .mobile-menu-item > .link').off('click').on('click', function()
$(this).closest('.mobile-menu-item').toggleClass('-active');
)
,

clear : function()
$('.primary-mobile-menu, .mobile-menu-item').removeClass('primary-mobile-menu mobile-menu-item');



var addHash =

init : function()

if ($('.region-primary-menu').hasClass('primary-mobile-menu'))

$('.primary-mobile-menu .mobile-menu-item > .link').each(function()
// console.log($(this).attr('href'));
let currentUrl = $(this).attr('href');

$(this).prop('href', '#' + currentUrl);

// this.href = '/#/' + this.href;
// window.location.hash = $(this).attr('href');
);


else

$('.primary-mobile-menu .mobile-menu-item > .link').each(function()

$(this).removeAttr('#');

);






$(document).ready(function()

if ($(window).outerWidth() <= 1024)
mobileMenuDrawer.init();


else
mobileMenuDrawer.clear();


addHash.init();


);

$(window).on('resize', function()

if ($(window).outerWidth() <= 1024)
mobileMenuDrawer.init();
addHash.init();


else
mobileMenuDrawer.clear();

);



)(jQuery)









share|improve this question


























    2















    Is there a way to fire jquery only once when resizing? I have here a script that adds a hash(#) within the href of an anchor <a> element. The problem is the script keeps when resizing and so it keeps adding hash within the href. Is there a way to make it fire only once?



    (function($)
    'use strict';



    var mobileMenuDrawer =

    init : function()

    $('.region-primary-menu').addClass('primary-mobile-menu');
    $('.primary-mobile-menu .menu.-primary > .menu-item').addClass('mobile-menu-item');
    $('.primary-mobile-menu .menu.-primary > .mobile-menu-item > .link').off('click').on('click', function()
    $(this).closest('.mobile-menu-item').toggleClass('-active');
    )
    ,

    clear : function()
    $('.primary-mobile-menu, .mobile-menu-item').removeClass('primary-mobile-menu mobile-menu-item');



    var addHash =

    init : function()

    if ($('.region-primary-menu').hasClass('primary-mobile-menu'))

    $('.primary-mobile-menu .mobile-menu-item > .link').each(function()
    // console.log($(this).attr('href'));
    let currentUrl = $(this).attr('href');

    $(this).prop('href', '#' + currentUrl);

    // this.href = '/#/' + this.href;
    // window.location.hash = $(this).attr('href');
    );


    else

    $('.primary-mobile-menu .mobile-menu-item > .link').each(function()

    $(this).removeAttr('#');

    );






    $(document).ready(function()

    if ($(window).outerWidth() <= 1024)
    mobileMenuDrawer.init();


    else
    mobileMenuDrawer.clear();


    addHash.init();


    );

    $(window).on('resize', function()

    if ($(window).outerWidth() <= 1024)
    mobileMenuDrawer.init();
    addHash.init();


    else
    mobileMenuDrawer.clear();

    );



    )(jQuery)









    share|improve this question
























      2












      2








      2








      Is there a way to fire jquery only once when resizing? I have here a script that adds a hash(#) within the href of an anchor <a> element. The problem is the script keeps when resizing and so it keeps adding hash within the href. Is there a way to make it fire only once?



      (function($)
      'use strict';



      var mobileMenuDrawer =

      init : function()

      $('.region-primary-menu').addClass('primary-mobile-menu');
      $('.primary-mobile-menu .menu.-primary > .menu-item').addClass('mobile-menu-item');
      $('.primary-mobile-menu .menu.-primary > .mobile-menu-item > .link').off('click').on('click', function()
      $(this).closest('.mobile-menu-item').toggleClass('-active');
      )
      ,

      clear : function()
      $('.primary-mobile-menu, .mobile-menu-item').removeClass('primary-mobile-menu mobile-menu-item');



      var addHash =

      init : function()

      if ($('.region-primary-menu').hasClass('primary-mobile-menu'))

      $('.primary-mobile-menu .mobile-menu-item > .link').each(function()
      // console.log($(this).attr('href'));
      let currentUrl = $(this).attr('href');

      $(this).prop('href', '#' + currentUrl);

      // this.href = '/#/' + this.href;
      // window.location.hash = $(this).attr('href');
      );


      else

      $('.primary-mobile-menu .mobile-menu-item > .link').each(function()

      $(this).removeAttr('#');

      );






      $(document).ready(function()

      if ($(window).outerWidth() <= 1024)
      mobileMenuDrawer.init();


      else
      mobileMenuDrawer.clear();


      addHash.init();


      );

      $(window).on('resize', function()

      if ($(window).outerWidth() <= 1024)
      mobileMenuDrawer.init();
      addHash.init();


      else
      mobileMenuDrawer.clear();

      );



      )(jQuery)









      share|improve this question














      Is there a way to fire jquery only once when resizing? I have here a script that adds a hash(#) within the href of an anchor <a> element. The problem is the script keeps when resizing and so it keeps adding hash within the href. Is there a way to make it fire only once?



      (function($)
      'use strict';



      var mobileMenuDrawer =

      init : function()

      $('.region-primary-menu').addClass('primary-mobile-menu');
      $('.primary-mobile-menu .menu.-primary > .menu-item').addClass('mobile-menu-item');
      $('.primary-mobile-menu .menu.-primary > .mobile-menu-item > .link').off('click').on('click', function()
      $(this).closest('.mobile-menu-item').toggleClass('-active');
      )
      ,

      clear : function()
      $('.primary-mobile-menu, .mobile-menu-item').removeClass('primary-mobile-menu mobile-menu-item');



      var addHash =

      init : function()

      if ($('.region-primary-menu').hasClass('primary-mobile-menu'))

      $('.primary-mobile-menu .mobile-menu-item > .link').each(function()
      // console.log($(this).attr('href'));
      let currentUrl = $(this).attr('href');

      $(this).prop('href', '#' + currentUrl);

      // this.href = '/#/' + this.href;
      // window.location.hash = $(this).attr('href');
      );


      else

      $('.primary-mobile-menu .mobile-menu-item > .link').each(function()

      $(this).removeAttr('#');

      );






      $(document).ready(function()

      if ($(window).outerWidth() <= 1024)
      mobileMenuDrawer.init();


      else
      mobileMenuDrawer.clear();


      addHash.init();


      );

      $(window).on('resize', function()

      if ($(window).outerWidth() <= 1024)
      mobileMenuDrawer.init();
      addHash.init();


      else
      mobileMenuDrawer.clear();

      );



      )(jQuery)






      javascript jquery






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 0:14









      clestcruzclestcruz

      3551337




      3551337






















          1 Answer
          1






          active

          oldest

          votes


















          2














          Yes. Just put a flag checking if the property already exists:



          if(!$(this).prop('href')) 
          $(this).prop('href', '#' + currentUrl);



          Or if the property is similar:



          if($(this).prop('href') != '#' + currentUrl) 
          $(this).prop('href', '#' + currentUrl);



          Or if the property is empty (will only work if the property already exists):



          if($(this).prop('href') == "") 
          $(this).prop('href', '#' + currentUrl);




          If you want to prevent the entire codeblock from executing. You could use some sort of custom flag that you need to flick to the opposite boolean value once a desired operation has been completed. You could make this a variable in your local function scope or make it a property of the addHash object. I'll do the former in this one.



           let isOk = false;

          var addHash =

          init : function()
          if(isOk) return;

          if ($('.region-primary-menu').hasClass('primary-mobile-menu'))

          $('.primary-mobile-menu .mobile-menu-item > .link').each(function()
          let currentUrl = $(this).attr('href');

          $(this).prop('href', '#' + currentUrl);
          isOk = true;
          );






          Note: You should stop using var.






          share|improve this answer

























          • I tried the first solution by adding a flag or condition, but it still keeps on adding a hash when I tried resizing. Made a jsfiddle you could check on it. jsfiddle.net/eof8kpsj

            – clestcruz
            Nov 14 '18 at 0:49












          • What I'm just trying resolve here is just to figure out how I can fire the script only once when the window is being resize below 1024px so it stops adding hash to the href

            – clestcruz
            Nov 14 '18 at 1:01











          • @clestcruz I edited a bit and any of my solutions should now work.

            – Abana Clara
            Nov 14 '18 at 1:02











          • Your second solution has an extra parentheses () I'm trying out the second solution and is still adds hash which is weird since the purpose of the flag is to check the value

            – clestcruz
            Nov 14 '18 at 1:09











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291352%2ftrying-to-set-jquery-to-fire-once-after-resizing%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Yes. Just put a flag checking if the property already exists:



          if(!$(this).prop('href')) 
          $(this).prop('href', '#' + currentUrl);



          Or if the property is similar:



          if($(this).prop('href') != '#' + currentUrl) 
          $(this).prop('href', '#' + currentUrl);



          Or if the property is empty (will only work if the property already exists):



          if($(this).prop('href') == "") 
          $(this).prop('href', '#' + currentUrl);




          If you want to prevent the entire codeblock from executing. You could use some sort of custom flag that you need to flick to the opposite boolean value once a desired operation has been completed. You could make this a variable in your local function scope or make it a property of the addHash object. I'll do the former in this one.



           let isOk = false;

          var addHash =

          init : function()
          if(isOk) return;

          if ($('.region-primary-menu').hasClass('primary-mobile-menu'))

          $('.primary-mobile-menu .mobile-menu-item > .link').each(function()
          let currentUrl = $(this).attr('href');

          $(this).prop('href', '#' + currentUrl);
          isOk = true;
          );






          Note: You should stop using var.






          share|improve this answer

























          • I tried the first solution by adding a flag or condition, but it still keeps on adding a hash when I tried resizing. Made a jsfiddle you could check on it. jsfiddle.net/eof8kpsj

            – clestcruz
            Nov 14 '18 at 0:49












          • What I'm just trying resolve here is just to figure out how I can fire the script only once when the window is being resize below 1024px so it stops adding hash to the href

            – clestcruz
            Nov 14 '18 at 1:01











          • @clestcruz I edited a bit and any of my solutions should now work.

            – Abana Clara
            Nov 14 '18 at 1:02











          • Your second solution has an extra parentheses () I'm trying out the second solution and is still adds hash which is weird since the purpose of the flag is to check the value

            – clestcruz
            Nov 14 '18 at 1:09
















          2














          Yes. Just put a flag checking if the property already exists:



          if(!$(this).prop('href')) 
          $(this).prop('href', '#' + currentUrl);



          Or if the property is similar:



          if($(this).prop('href') != '#' + currentUrl) 
          $(this).prop('href', '#' + currentUrl);



          Or if the property is empty (will only work if the property already exists):



          if($(this).prop('href') == "") 
          $(this).prop('href', '#' + currentUrl);




          If you want to prevent the entire codeblock from executing. You could use some sort of custom flag that you need to flick to the opposite boolean value once a desired operation has been completed. You could make this a variable in your local function scope or make it a property of the addHash object. I'll do the former in this one.



           let isOk = false;

          var addHash =

          init : function()
          if(isOk) return;

          if ($('.region-primary-menu').hasClass('primary-mobile-menu'))

          $('.primary-mobile-menu .mobile-menu-item > .link').each(function()
          let currentUrl = $(this).attr('href');

          $(this).prop('href', '#' + currentUrl);
          isOk = true;
          );






          Note: You should stop using var.






          share|improve this answer

























          • I tried the first solution by adding a flag or condition, but it still keeps on adding a hash when I tried resizing. Made a jsfiddle you could check on it. jsfiddle.net/eof8kpsj

            – clestcruz
            Nov 14 '18 at 0:49












          • What I'm just trying resolve here is just to figure out how I can fire the script only once when the window is being resize below 1024px so it stops adding hash to the href

            – clestcruz
            Nov 14 '18 at 1:01











          • @clestcruz I edited a bit and any of my solutions should now work.

            – Abana Clara
            Nov 14 '18 at 1:02











          • Your second solution has an extra parentheses () I'm trying out the second solution and is still adds hash which is weird since the purpose of the flag is to check the value

            – clestcruz
            Nov 14 '18 at 1:09














          2












          2








          2







          Yes. Just put a flag checking if the property already exists:



          if(!$(this).prop('href')) 
          $(this).prop('href', '#' + currentUrl);



          Or if the property is similar:



          if($(this).prop('href') != '#' + currentUrl) 
          $(this).prop('href', '#' + currentUrl);



          Or if the property is empty (will only work if the property already exists):



          if($(this).prop('href') == "") 
          $(this).prop('href', '#' + currentUrl);




          If you want to prevent the entire codeblock from executing. You could use some sort of custom flag that you need to flick to the opposite boolean value once a desired operation has been completed. You could make this a variable in your local function scope or make it a property of the addHash object. I'll do the former in this one.



           let isOk = false;

          var addHash =

          init : function()
          if(isOk) return;

          if ($('.region-primary-menu').hasClass('primary-mobile-menu'))

          $('.primary-mobile-menu .mobile-menu-item > .link').each(function()
          let currentUrl = $(this).attr('href');

          $(this).prop('href', '#' + currentUrl);
          isOk = true;
          );






          Note: You should stop using var.






          share|improve this answer















          Yes. Just put a flag checking if the property already exists:



          if(!$(this).prop('href')) 
          $(this).prop('href', '#' + currentUrl);



          Or if the property is similar:



          if($(this).prop('href') != '#' + currentUrl) 
          $(this).prop('href', '#' + currentUrl);



          Or if the property is empty (will only work if the property already exists):



          if($(this).prop('href') == "") 
          $(this).prop('href', '#' + currentUrl);




          If you want to prevent the entire codeblock from executing. You could use some sort of custom flag that you need to flick to the opposite boolean value once a desired operation has been completed. You could make this a variable in your local function scope or make it a property of the addHash object. I'll do the former in this one.



           let isOk = false;

          var addHash =

          init : function()
          if(isOk) return;

          if ($('.region-primary-menu').hasClass('primary-mobile-menu'))

          $('.primary-mobile-menu .mobile-menu-item > .link').each(function()
          let currentUrl = $(this).attr('href');

          $(this).prop('href', '#' + currentUrl);
          isOk = true;
          );






          Note: You should stop using var.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 1:39

























          answered Nov 14 '18 at 0:19









          Abana ClaraAbana Clara

          1,621919




          1,621919












          • I tried the first solution by adding a flag or condition, but it still keeps on adding a hash when I tried resizing. Made a jsfiddle you could check on it. jsfiddle.net/eof8kpsj

            – clestcruz
            Nov 14 '18 at 0:49












          • What I'm just trying resolve here is just to figure out how I can fire the script only once when the window is being resize below 1024px so it stops adding hash to the href

            – clestcruz
            Nov 14 '18 at 1:01











          • @clestcruz I edited a bit and any of my solutions should now work.

            – Abana Clara
            Nov 14 '18 at 1:02











          • Your second solution has an extra parentheses () I'm trying out the second solution and is still adds hash which is weird since the purpose of the flag is to check the value

            – clestcruz
            Nov 14 '18 at 1:09


















          • I tried the first solution by adding a flag or condition, but it still keeps on adding a hash when I tried resizing. Made a jsfiddle you could check on it. jsfiddle.net/eof8kpsj

            – clestcruz
            Nov 14 '18 at 0:49












          • What I'm just trying resolve here is just to figure out how I can fire the script only once when the window is being resize below 1024px so it stops adding hash to the href

            – clestcruz
            Nov 14 '18 at 1:01











          • @clestcruz I edited a bit and any of my solutions should now work.

            – Abana Clara
            Nov 14 '18 at 1:02











          • Your second solution has an extra parentheses () I'm trying out the second solution and is still adds hash which is weird since the purpose of the flag is to check the value

            – clestcruz
            Nov 14 '18 at 1:09

















          I tried the first solution by adding a flag or condition, but it still keeps on adding a hash when I tried resizing. Made a jsfiddle you could check on it. jsfiddle.net/eof8kpsj

          – clestcruz
          Nov 14 '18 at 0:49






          I tried the first solution by adding a flag or condition, but it still keeps on adding a hash when I tried resizing. Made a jsfiddle you could check on it. jsfiddle.net/eof8kpsj

          – clestcruz
          Nov 14 '18 at 0:49














          What I'm just trying resolve here is just to figure out how I can fire the script only once when the window is being resize below 1024px so it stops adding hash to the href

          – clestcruz
          Nov 14 '18 at 1:01





          What I'm just trying resolve here is just to figure out how I can fire the script only once when the window is being resize below 1024px so it stops adding hash to the href

          – clestcruz
          Nov 14 '18 at 1:01













          @clestcruz I edited a bit and any of my solutions should now work.

          – Abana Clara
          Nov 14 '18 at 1:02





          @clestcruz I edited a bit and any of my solutions should now work.

          – Abana Clara
          Nov 14 '18 at 1:02













          Your second solution has an extra parentheses () I'm trying out the second solution and is still adds hash which is weird since the purpose of the flag is to check the value

          – clestcruz
          Nov 14 '18 at 1:09






          Your second solution has an extra parentheses () I'm trying out the second solution and is still adds hash which is weird since the purpose of the flag is to check the value

          – clestcruz
          Nov 14 '18 at 1:09


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291352%2ftrying-to-set-jquery-to-fire-once-after-resizing%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto