Trying to set jquery to fire once after resizing
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
add a comment |
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
add a comment |
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
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
javascript jquery
asked Nov 14 '18 at 0:14
clestcruzclestcruz
3551337
3551337
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
I tried the first solution by adding aflag
orcondition
, but it still keeps on adding ahash
when I tried resizing. Made ajsfiddle
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 below1024px
so it stops addinghash
to thehref
– 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 addshash
which is weird since the purpose of theflag
is to check the value
– clestcruz
Nov 14 '18 at 1:09
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
I tried the first solution by adding aflag
orcondition
, but it still keeps on adding ahash
when I tried resizing. Made ajsfiddle
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 below1024px
so it stops addinghash
to thehref
– 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 addshash
which is weird since the purpose of theflag
is to check the value
– clestcruz
Nov 14 '18 at 1:09
add a comment |
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.
I tried the first solution by adding aflag
orcondition
, but it still keeps on adding ahash
when I tried resizing. Made ajsfiddle
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 below1024px
so it stops addinghash
to thehref
– 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 addshash
which is weird since the purpose of theflag
is to check the value
– clestcruz
Nov 14 '18 at 1:09
add a comment |
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.
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.
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 aflag
orcondition
, but it still keeps on adding ahash
when I tried resizing. Made ajsfiddle
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 below1024px
so it stops addinghash
to thehref
– 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 addshash
which is weird since the purpose of theflag
is to check the value
– clestcruz
Nov 14 '18 at 1:09
add a comment |
I tried the first solution by adding aflag
orcondition
, but it still keeps on adding ahash
when I tried resizing. Made ajsfiddle
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 below1024px
so it stops addinghash
to thehref
– 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 addshash
which is weird since the purpose of theflag
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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