Basically, if the URL/window.location contains absolutely any variable whatsoever (past domain.com/, of course), I'd like javascript to execute something.
Currently, I have the following jQuery code which only executes when window.location contains the exact wording "#hash", but as stated before I'd like to expand the functionality for all variables.
Edit: Sorry, to clarify, by variable I mean any one of the following examples:
domain.com/#hash
domain.com/#hash2
domain.com/sub/folder
domain.com/textwithoutahash
Also, if someone knows how to do this in basic Javascript and without the need for the jQuery library, that would be an added bonus!
$(function() {
if ( window.location.href.indexOf('#hash') > -1 ) {
myfunctionhere;
}
});
See update at end re your clarification
Put the script at the end of the page, just before the closing </body>, and:
If by "variable" you mean a document fragment identifier ("hash"), then:
<script>
if (location.hash) {
callYourFunction();
}
</script>
If by "variable" you mean a query string, then
<script>
if (location.search) {
callYourFunction();
}
</script>
If by "variable" you mean a resource name, e.g., not http://domain.com but http://domain.com/page, then:
<script>
if (location.pathname && location.pathname !== "/") {
callYourFunction();
}
</script>
More on the location object on MDN.
Re your clarification:
Edit: Sorry, to clarify, by variable I mean any one of the following examples:
Those examples come down to having either hash or pathname or both, so:
<script>
if ((location.pathname && location.pathname !== "/") || location.hash) {
callYourFunction();
}
</script>
...and of course, if you also wanted to handle http://domain.com?foo=bar, then add in search as well:
<script>
if ((location.pathname && location.pathname !== "/") ||
location.search ||
location.hash) {
callYourFunction();
}
</script>
You could check if there is a hash, a pathname or a search.
Or, to simplify, you could simply use this:
if (window.location.href.split('/').filter(Boolean).length > 2) {
callYourFunction();
}
window.location.href is simply the whole URL. If there's something after the domain, it'll be shown.
This function will be triggered for the following cases:
domain.com/some/path
domain.com/#hash
domain.com/?some=variable
You could check if search property of window.location is set to something. Also, you can check the hash property:
if (window.location.search || window.location.hash) {
yourfunctionhere();
}
To invoke it without jQuery, just include it in an 'onload' script:
<script type='text/javascript'>
document.onload = function () {
if (window.location.search || window.location.hash) {
yourfunctionhere();
}
}
</script>
Related
I have a form with id commentform and if any logged in user visit the page a p tag gets generated under the form that with class logged-in-as. Now I am trying to check if that p exists and if not exists then do my validation which uses keyup(). Here is a small snippet...
$('form#commentform').keyup(function() {
if( ! $(this).has('p').hasClass('logged-in-as') ) {
....
} else {
......
}
}
});
Now the problem is that the if( ! $(this).has('p').hasClass('logged-in-as') ) is not returning me the expected result whether or not that specific p exists.
Can any of you guys tell me any other/better way to check this?
$('form#commentform').keyup(function() {
if($(this).find('p.logged-in-as').length == 1) {
....
} else {
......
}
}
});
You can do this to find it.
You can use
if ($('.logged-in-as', this).length)) {
But I would rather use a variable to store that state instead of relying on checking the presence of a raw tag : what if you change your HTML a little ?
Side note: Don't use overqualified selectors. $('#commentform') is faster and logically more consistent than $('form#commentform').
Check if an element witth class "xxx" exist
if( $( ".xxx" ).size() > 0 ) {
// EXISTS
}
Edit: forgot the dot ( ".xxx" )
Very simple question but can't find the answer. Can you put two url's inside
if (window.location.href
here is mine:
if (window.location.href == 'http://example.example.com/support/default.asp') {
}
I need to add a second link... so i only want a div to load from that page and one more page.
It should be as simples as
if (window.location.href == 'http://example.example.com/support/default.asp' ||
window.location.href == 'http://secondurl.com') {
}
unless I misunderstood the problem.
You can use || (or) in if loop.
if (window.location.href == 'http://example.example.com/support/default.asp'
|| window.location.href == 'http://example2.com') {
}
I have the following code block which works great:
jQuery(".archive-job_listing-layout").click(function(evt) {
evt.preventDefault();
if (!jQuery("body").hasClass('post-type-archive-job_listing'))
return;
console.log("Click " + jQuery(this).data('style'));
console.log(jQuery(window).width());
if (jQuery(this).data('style') == "grid" && jQuery(window).width() < 800) {
jQuery("ul.job_listings").css('display','block');
jQuery("table#wswp-header-row").hide().remove();
jQuery(".table_padding").hide().remove();
return;
}
layout_to_table("click");
})
});
I want to do is add another line which like:
if (!jQuery("body").hasClass('archive tax-job_listing_type'))
return;
but adding this breaks the code. I've tried using If Else, Or (||) And (&&), but nothing works.
If i substitute 'post-type-archive-job_listing' with 'archive tax-job_listing_type' the code also works fine, i just can't seem to get both of these lines of code to work at the same time.
This should work:
if(!jQuery("body").hasClass('archive tax-job_listing_type') && !jQuery("body").hasClass('post-type-archive-job_listing'))
return;
Perhaps separating with a few more parenthesis will work out for you:
if (!(jQuery("body").hasClass('post-type-archive-job_listing')) || !(jQuery("body").hasClass('archive tax-job_listing_type')))
return;
Can use is() which accepts multiple selectors. Will act like or when more than one selector is passed to it
if(!jQuery("body").is('.archive tax-job_listing_type, .post-type-archive-job_listing'))
DEMO
I have the following if statement that looks for a hash on document ready (this code NEVER runs again on the page). And if there is no hash then add one using replace, so that it doesn't trigger a hashchange event.
if( window.location.hash == '' ) {
console.log('no hash');
location.replace(location.href.replace(/\/?$/, '#/section-0/page-0')); // change the url without creating a hashchange
} else {
console.log('has hash');
}
However if I visit the page with no hash, what I will see in the console is has hash and the replace will have happened... how is this possible? as that console log is in a different part of the statement. If I comment out the replace then it only falls into the first part of the if statement. How can it jump into the if statement do the replace (but ignoring the first console log) and then jump into the second part?
What you are saying doesn't make sense.
I tried to make a full example from your code:
<html>
<head>
<script type='text/javascript'>
function x()
{
if( window.location.hash == '' )
{
console.log('no hash');
location.replace(location.href.replace(/\/?$/, '#/section-0/page-0')); // change the url without creating a hashchange
alert('changed!');
}
else
{
console.log('has hash');
}
}
</script>
</head>
<body>
<button onclick="javascript:x()">test</button>
</body>
</html>
This code executes as follows when opening default:
Click button
Console no hash
Alert
Click button
Console has hash
If you put the code without function declaration inside the body (so it always executes), like this:
<html>
<body>
<script type='text/javascript'>
if( window.location.hash == '' )
{
console.log('no hash');
location.replace(location.href.replace(/\/?$/, '#/section-0/page-0')); // change the url without creating a hashchange
alert('changed!');
}
else
{
console.log('has hash');
}
</script>
</body>
</html>
It shows:
Console no hash
Alert
Is there some way I can tell whether window.location.hash is defined?
If it is set, then I will be getting the value of the variable, and using it to display extra content on the page.
if(window.location.hash) {
// do stuff!
}
How about:
if(window.location.hash !== '')
{
}
Or
if(typeof window.location.hash !== 'undefined')
{
//your code
}