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
}
Related
I'm trying to prevent the space button from being pressed on all of my pages except 2 where I have textareas. I need spaces on these textarea pages so the user can enter in their support ticket message. I have tried many different ways of doing this and cannot find a working way. I've spent about 1 whole week just trying this small error. I thought it was time to get some help.
The error is that when I use the code below, it tells me that I cannot use spaces (and prevents spaces) eventhough I am on one of the pages I said not to run the script on.
My current code is :
$(document).keydown(function(event) {
var myUrl_one = '/panel/staff?page=tickets';
var myUrl_two = '/panel/support';
var currentUrl = window.location.pathname;
if(currentUrl != myUrl_one || currentUrl != myUrl_two) {
if (event.which == 32) {
event.preventDefault();
$.Notification.autoHideNotify('error', 'top right', 'Keyboard Error!', 'No spaces are allowed here!');
}
}
});
Thanks to #RogerCageot and #Airwavezx, I have found the issue.
Firstly, I needed to change my || operator to && in my if statement like so :
if(currentUrl != myUrl_one && currentUrl != myUrl_two) {
Secondly, the javascript function window.location.pathname only gets the raw path location, not GET parameters. Because I was trying to get /panel/staff?page=tickets, it wasn't reading the ?page=tickets. It was only reading the /panel/staff.
My fully working code is below :
$(document).keydown(function(event) {
var myUrl_one = '/panel/staff';
var myUrl_two = '/panel/support';
var currentUrl = window.location.pathname;
if(currentUrl != myUrl_one && currentUrl != myUrl_two) {
if (event.which == 32) {
event.preventDefault();
$.Notification.autoHideNotify('error', 'top right', 'Keyboard Error!', 'No spaces are allowed here!');
console.log(currentUrl, myUrl_one);
}
}
});
I'm making a chrome extension and what I want to happen is to alert the user if a specific link has been clicked on Facebook. And in my script every time I click a link it always alert accessing gma.
$(document).on("click", "a", function() {
//this == the link that was clicked
var href = $(this).attr("href");
if (window.location.protocol == 'https:'){
if(href == "gmanetwork"){
alert("Accessing Gma");
}}
else{
alert("false");
}
});
I would suggest something like this:
You change your html links to :
Your link
and your script:
$(document).on("click", "a", function() {
if($(this).attr("targetLink"){
alert("You are going to the following page: " + $(this).attr("targetLink"));
}
});
Code at Question assigns the value if (window.location.protocol = 'https:'){ "gmanetwork" to href using = operator at
if (href = "gmanetwork")
instead of checking the values for equality, use === operator
if (window.location.protocol === 'https:') {
if (href === "gmanetwork") {
alert("Accessing Gma");
}
}
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'm looking for a way to rewrite URL of the location when the user want's to change page. So, let's say you have something like this:
<body>
<a href="http://example.com" />
</body>
Is there a way I can catch URL changing moment, and actually modify that URL before location is changed, for example I would like to change href into relative link like \http://example.com and redirect page actually there.
If you just want to trap the link and then modify it then yes, that's quite simple...
$("a").on("click", function(e) {
e.preventDefault(); // stops the link doing its default thing
window.location.href = "something/" + $(this).attr("href");
});
You obviously need to modify the line that changes the location, so that it modifies the href value however you need. I'd also recommend giving the links a class and selecting them with that, as the above code will affect every link on the page.
Finally, this will need to run after the DOM is loaded, so either wrap it in a document.ready handler of your choice, or put it in a script at the bottom of the body.
Demo
You can work from here. Also you will need urlrewrite in htaccess for this to work properly.
$(function () {
$('.buttonn').on('click', function (e) {
var seperator = (window.location.href.indexOf("?") === -1) ? "?" : "&";
if (window.location.href.indexOf("s1") === -1 && window.location.href.indexOf("s2") != -1) {
window.location.href = window.location.href.replace(/&?s2=([^&]$|[^&]*)/i, "&s1=s1");
} else if (window.location.href.indexOf("s1") != -1) {
window.location.href = window.location.href.replace(/&?s1=([^&]$|[^&]*)/i, "&s1=s1");
} else {
window.location.href = window.location.href + seperator + "s1=s1";
}
});
});
$(function () {
$('.buttono').on('click', function (e) {
var seperator = (window.location.href.indexOf("?") === -1) ? "?" : "&";
if (window.location.href.indexOf("s2") === -1 && window.location.href.indexOf("s1") != -1) {
window.location.href = window.location.href.replace(/&?s1=([^&]$|[^&]*)/i, "&s2=s2");
} else if (window.location.href.indexOf("s2") != -1) {
window.location.href = window.location.href.replace(/&?s2=([^&]$|[^&]*)/i, "&s2=s2");
} else {
window.location.href = window.location.href + seperator + "s2=s2";
}
});
});
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';
}