jQuery - get id of url and do something (if/else) [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to accomplish a 'simple' jQuery effect.
When an user loads their dashboard, and they have a specific id (#) in the url, the dashboard should load the content assigned to that specific id.
Here is what I have so far:
var pathname = window.location.hash.substr(1);
if(pathname = loadextra){
$('.loadextra').addClass('active');
$('.load-fixed').removeClass('active');
$('.fixed-ads').hide();
$('#loader').show().html('<div class="typing_loader_holder"><div class="typing_loader"></div></div>');
setTimeout(
function()
{
$('#loader').hide();
$('.extra-ads').show();
}, 2000);
}elseif(pathname = loadfixed){
$(this).addClass('active');
$('.load-extra').removeClass('active');
$('.extra-ads').hide();
$('#loader').show().html('<div class="typing_loader_holder"><div class="typing_loader"></div></div>');
setTimeout(
function()
{
$('#loader').hide();
$('.fixed-ads').show();
}, 2000);
}else{
//do nothing
}
So, imagine an user goes to: http://website.com/user#loadextra then the code inside if(pathname = loadextra){} should be fired off - same goes for #loadfixed
Although, as it is now, if you just goes to http://website.com/user, then if(pathname = loadextra){} is fired of.
Why doesn't the if/elseif/else statement work in my code?

You have make three mistakes:
1) Replace '=' with '==' in if & elseif
2) Replace 'elseif' with 'else if'
3)Replace $(this).addClass('active'); with $('.loadfixed').addClass('active');

Try this:
if(pathname == loadextra)

Related

How to avoid 'Uncaught ReferenceError: ? is undefined'? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a script (config.js) at the base of every page. But, that script contains code that isn't used on every page. The console returns unused or undeclared variables as undefined.
let currentURL = document.location.href;
function redirectURL() {
if (currentURL.indexOf('dogs.html') > -1) {
redirect.innerHTML = `Cats`;
} else {
redirect.innerHTML = `Dogs`;
}
}
redirectURL();
This is what's returned.
How do I fix this?
You need to check whether the redirect element exists before trying to use it.
function redirectURL() {
if (typeof redirect == 'undefined') {
return;
}
if (currentURL.indexOf('dogs.html') > -1) {
redirect.innerHTML = `Cats`;
} else {
redirect.innerHTML = `Dogs`;
}
}
This will allow the function to work without error on pages that don't define redirect.

hasClass is not a function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm writing a simple jquery code to animation an element on my page:
curent
$('#tgs-nav-icon').on('click', function() {
var $nav = (".canvas-menu-content");
var bounceInRight = "bounceInRight animated";
if(! $nav.hasClass(bounceInRight) ) {
$nav.addClass(bounceInRight);
}
else {
$nav.removeClass(bounceInRight);
}
});
Uncaught TypeError: nav.hasclass is not a function
I tried testing this to see where I went wrong and this code comes back false as it should:
var help = $( ".canvas-menu-content" );
console.log(help.hasClass("foo"));
I can't figure out why the if statement is throwing me the error.
I've tried doing some research, but none of the previous questions seem to answer my issue.
You're assigning $nav to the string ".canvas-menu-content" here:
var $nav = (".canvas-menu-content"); // $nav is now '.canvas-menu-content'
Assign it to a jQuery object instead:
var $nav = $(".canvas-menu-content"); // $nav is now a jQuery object containing all
// elements with class canvas-menu-content

jQuery call incorrectly firing at the same time as fade [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am building a lightbox and am running into an issue where the following fadeIn function is firing at the same time as the fadeOut instead of afterwards.
Why is updateImage.call(this) firing at the same time as the fadeOut? Should it not fire afterwards considering it is placed as a callback?
Full code snippet
function updateImage() {
activeImage = overlayImagesBox.find('.' + this.className);
activeImage.fadeIn('slow').addClass('active-image');
}
imageLinks.on('click', function (e) {
e.preventDefault();
if (!activeImage) {
updateImage.call(this);
} else {
activeImage.removeClass('active-image').fadeOut('slow', updateImage.call(this));
activeImage = null;
}
});
As #blex mentioned in the comments the correct answer is simply passing the function as a callback instead of executing it. Thank you for the help everyone.

how to find and replace text url with links in jquery [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Hi I want to find text url in my editor and convert them to anchor tag links using jquery.
here is my code
function urlify(text) {
var urlRegex = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
return text.replace(urlRegex, function(url) {
return '' + url + '';
})
}
$("#queEditor").bind('input propertychange', function(){
var url = urlify($("#queEditor").text);
console.log(url);
});
But it's giving an error undefined function text. Could someone help me rectify this?
Since .text() is a function you need to use () when you want to invoke it.
$("#queEditor").text(); //Notice ()

Jquery not behaving as expected [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
First I echoed a button using PHP with value='pause'.Then I wrote a simple if statement in jquery
$(":button").click(function(){
if($(this).attr("value") == "pause"){
alert($(this).attr("value"));
$(this).attr("value") = "play";
}else{
alert($(this).attr("value"));
$(this).attr("value") = "pause";
}
});
But the result is always alert("pause");
What I am trying to do is if I click the button with value='pause', i want to execute an ajax request and change its value to 'play' so that on the next click it will execute another bit of code and set its value back to pause.
This code is meaningless:
$(this).attr("value") = "pause";
That probably generated errors that you're not paying attention to. When working with JavaScript code, always have your console open.
What you want is:
$(this).attr("value", "pause");
Even better, as IFun points out:
$(this).val("pause");
Make sure to consult the attr() documentation.

Categories

Resources