Javascript if (window.location.href) adding two urls - javascript

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') {
}

Related

Adding a class with multiple links in jquery

I'm adding an background image for third party links with filter function like this
$("a").filter(function () {
var ignoreLink =
this.hostname == location.hostname // disregard same domain
|| this.href.match(/^(mailto|tel|javascript)\:/)
return !ignoreLink;
}).addClass("externalanchor").attr("target", "_blank");
the above code works fine now i have a requirement saying that some of the links should be internal ex:
<p>Email URL</p>
<p>Google</p>
<p>Google</p>
<p>Google</p>
my question is How to add a class for this links separate with out a dom change only with jquery.
You can use attribute contains selector
$("a[href*='google.com'], a[href*='w3schools.com'], a[href*='codeacademy.com']").addClass('no-external');
Maybe something like this works for you?:
https://jsfiddle.net/khkmmjjn/1/
Code:
$("a").each(function(){
if( $(this).attr("href").indexOf("https://jsfiddle.net") == 0 ||
$(this).attr("href").indexOf("jsfiddle.net") == 0 )
{
$(this).addClass("local");
}
else if( $(this).attr("href").indexOf("mailto:") == 0 ||
$(this).attr("href").indexOf("tel:") == 0 )
{
$(this).addClass("other");
}
else
{
$(this).addClass("external");
}
});

javascript if condition trouble

I am trying to write this condition in javascript:
if(window.location.pathname != '/our-communities.php' && (window.location.pathname == '/upcoming-communities.php' && window.location.search != '')){
What I am trying to say is if the page is not our-communities.php and if the page is not upcoming-communities.php and window.location.search is not blank then run the code.
So this condition should run on every page except for our-communities.php but can run on upcoming-communities.php only if window.location.search is blank.
if (window.location.pathname != "/our-communities.php" || (window.location.pathname == "/upcoming-communities.php" && window.location.search == "")) {
}
One thing to note, if the page is "/upcoming-communities.php" then the first condition will always be true meaning the second OR statement is irrelevant, if you want to enforce that then extra conditionals are needed.
if ((window.location.pathname != "/our-communities.php" && window.location.pathname != "/upcoming-communities.php") || (window.location.pathname == "/upcoming-communities.php" && window.location.search == "")) {
}

If URL contains anything, then fire a Javascript function

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>

JavaScript browser agent redirect

I've got this little bit of code on my webpage header to redirect the user to a different page if they are using an iPhone:
<script type="text/javascript">
if ((navigator.userAgent.indexOf('iPhone') != -1) ||
(navigator.userAgent.indexOf('iPod') != -1)) {
document.location = "iPhone.aspx"; }
</script>
Is there an easy way to 'reverse' this code, so anyone landing on the iPhone page from ANY other browser will be redirected back to the home page?
Many thanks.
Simple solution. Replace your existing one with this.
if(!/(iphone|ipod)/i.test(navigator.userAgent)) {
window.location = "Desktop.aspx";
}
Updated since its only for iPhone page.
To reverse? Replace != -1 with == -1
Edit:
Suppose you mean something like this:
if ((navigator.userAgent.indexOf('iPhone') != -1) ||
(navigator.userAgent.indexOf('iPod') != -1)) {
document.location = "iPhone.aspx"; } else {
document.location = "notiPhone.aspx";
}
(navigator.userAgent.indexOf('iPhone') == -1 means there is no such string "iPhone" in the userAgent. So when you change from != to ==, you need to change || to && as well.
if ((navigator.userAgent.indexOf('iPhone') == -1)
&& (navigator.userAgent.indexOf('iPod') == -1)) {
document.location = 'default.aspx';
}

If statement with url plus universal variable

I'm trying to transform a blog on blogger into a website. In order to have a static home page I am using the Javascript code below to see if the user is on the home page if they are then it will hide the post section and display a home page "gadget". Is anything supposed to match anything?
document.onload = hidepage();
function hidepage () {
if (window.location == "http://website.blogspot.com/" || window.location == "http://website.blogspot.com/?zx=" + ANYTHING) {
//Checks to see if user is on the home page
$(".hentry").hide(); //Hide posts
$(".hfeed").hide(); //Hide posts
}
else {
$("#HTML2").hide(); //hide gadget
}
$(".post-title").hide(); //Hide post titles
}
Based on what you're saying I think you want to change the if condition to:
if (window.location.href === "http://website.blogspot.com/" ||
window.location.href.indexOf("http://website.blogspot.com/?zx=") > -1)
You could also shorten this to:
if (window.location.href === "http://website.blogspot.com/" ||
window.location.href.indexOf("/?zx=") > -1)
Note that I've changed your == to === as the latter is a literal comparison.
Just use String.indexOf in the second half of the if expression.
var url = window.location.href;
if (url === "http://website.blogspot.com/" || url.indexOf("http://website.blogspot.com/?zx=") === 0) {
// do stuff
}

Categories