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");
}
}
Related
Seems that people are clicking on Enter key on board and while I can disable that I have business analysts wanting me to have the Enter key "click" the Update link
Since this is asp.net gridview I don't have a lot of control of it plus there are many rows in which the __doPostBack data is different
How can I detect the word "Update" ( it was "Edit" along with many other rows having that Edit link as well)
Update
Update:
I want to find a keyword in an href that is in a table.
So "Update" is the keyword that is in href which is in a
<table>
<tr>
<td>
<a href..>Update</a>
</td>
</tr>
</table>
Update 2:
Here is a Fiddle
http://jsfiddle.net/bthorn/vzjLzfck/
I believe that this does FIND if update is there
var y = $('a').filter(function(index) { return $(this).text() === "Update"; });
I changed Update to "Updated" and length went from 1 to 0 ....
So then if that is working, I want to then proceed to have that Link executed or whatever do that essentially It is like someone is clicking on that href link
Final working code
$(document).keypress(function (e) {
if (e.which == 13) {
$("a").each(function () {
$("a").filter(function () {
var s = $(this).text() === "Update";
if (s == true) {
//console.log('yes in');
this.click();
}
});
return false;
});
return false;
}
});
If you want to target any href that contains the word "Update" you can use jQuery's filter function on the href
$("a").filter(function(){
return $(this).attr('href').indexOf("Update");
});
If you want the text to contain "Update" you can use:
$("a").filter(function(){
return $(this).text() === "Update";
});
$("body").on("keypress", function(e) {
if(e.which != 13) {
return;
};
$("a").each(function() {
var link = $(this);
if(link.text() == "Update") {
console.log("woot");
window.open(link.attr("href"));
};
});
});
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";
}
});
});
As soon as the web page loading is completes and whenever the cursor will be clicked/hover on any hyper links in that web page, I want to extract that particular link on which my mouse clicked/hover. So what is the JavaScript code snippet for this ?
window.onload = function() {
document.onclick = function(e) {
var target = e.srcElement;
if (target != null) {
var href = target.href;
if (href != null) {
alert("link extracted: " + href);
}
}
return false;
};
}
$(document).click(function(e){
if($(e.target).closest('a').length){
alert('You clicked a link');
alert($(e.target).closest('a').attr('href'));
}
else{
alert('You did not click a link');
}
});
I have a site that needs to verify like a cookie. but i want to use local storage since it is a mobile concept.
I know the item is set through a test but i cant get the page to redirect when it is not true.
<script language="javascript">
if (localStorage.getItem('itemVerified') = 'True') {
window.location = "success.html";
}
else {
alert("You are not able to enter this site!");
window.location = "error.html";
}
</script>
There may be two errors, the first of which is that your comparison string missing an = sign:
incorrect: if (localStorage.getItem('itemVerified') = 'True') {
Correct: if (localStorage.getItem('itemVerified') == 'True') {
The second case is if you're looking to check the site from which to load the document you need to add the action to an event listener for them you can do the following.
<script type="text/javascript">
var verifyCookie = function(){
if (localStorage.getItem('itemVerified') == 'True') {
console.log("All right");
window.location = "success.html";
}
else {
alert("You are not able to enter this site!");
window.location = "error.html";
}
};
window.addEventListener ('DOMContentLoaded', verifyCookie, false);
</script>
Regards.
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
}