I created a dynamic list with hyperlinks using Android PhoneGap. Now I want to check the link status (that is, whether the link is clicked or not) each time I run that application.
Add an attribute to link "onclick" and do something on onclick. There is also to be a counter variable to differentiate the links. Like this:
<a onclick="alert('clikced'+counter)">Link</a>
Or you can call any JavaScript function on the onclick event.
You can add event to every link, when it is clicked, count++, and save count to local storage or something else. When run the application next time, check out count for each link.
function linkisclicked(){
//Do your tasks
}
<a onclick="linkisclicked()"> link </a>
Related
I'm using the ChromeWebDriver together with Selenium. The application is partically controlled automatically. Now I want the user to navigate to a page, where he has to choose a link from some list like
<ul>
<li>Google</li>
<li>Ecosia</li>
<li>Yahoo</li>
</ul>
I want to get the link automatically, where the user clicked on. As example, when he clicks on Google, I need some kind of event, that gave me http://google.de in C#. There exist a WebDriverEventListener, his ElementClicked event is exactly what I need:
private void EventDriver_ElementClicked(object sender, WebElementEventArgs e) {
if(e.Element.TagName == "a") {
string link = e.Element.GetAttribute("href");
MessageBox.Show($"User has clicked on link {link}");
}
}
But the big problem here: ElementClicked is only fired on clicks triggered by Selenium using C#. All events have this issue. For example, Navigated got fired after calling driver.Navigate().GoToUrl("http://stackoverflow.com"), but not after manually clicking on a link in the browser.
To solve this, I think its necessary to forward client-side JS events. Like this pseudocode:
$('a').click(function() {
SeleniumBackend.NotifyAboutClickEvent('a', $(this));
});
I know there is a method called ExecuteScript which allows to run JS in the browser. It seems possible to catch a direct return like
string jsReturnValue = driver.ExecuteScript("return 'test';");
For this case, that's not enough, since some sort of callback would be needed to be async.
object clickedLink = sharedWebDriver.ExecuteAsyncScript(#"
var callback = arguments[arguments.length - 1];
clickedLink('http://google.de');
");
That works, issue here: Only once. I can't bind an eventhandler, which notify me about later clicks...
It looks like you are creating the initial menu page. If that's true, you can just set a JS global variable using an onlick event and then use JavascriptExecutor to grab the variable setting and then use that to determine what link they clicked.
...or better yet, you can just detect the browser URL after they click and know where they went.
I want a javascript to check if there is a button in the webpage that is being visited with the class detail-w-button act_watchlink like the following:
<a href="link" class="detail-w-button act_watchlink">
And if that button exists, I want to store in a variable the href.
How can I do this automatically when the page loads?
Update:
I don't know if it helps, but I know that the page has the following code to listen to the button:
$('.act_watchlink').on('click', function(){...});
I think it would be fine to just triggering that action automatically.
How to check if a node exists with jQuery:
if($('a.detail-w-button.act_watchlink').length > 0)
alert("I found it!");
else
alert("There is no such button");
Get href of this button:
var href = $('a.detail-w-button.act_watchlink').attr('href');
If you want to improve performance, store the button in a local variable instead of searching it each time you need it.
Update: If it is possible to encounter more than one such button on a page, you should address a specific item in the array of found objects. Like this:
var href = $('a.detail-w-button.act_watchlink').first().attr('href');
// note the first()
var href = $('a.detail-w-button.act_watchlink').eq(2).attr('href');
// note the eq(2)
$('.act_watchlink').trigger('click');
I have an
<a href= ./index2.html>
button in my index.html but now I want to change it so it also calls a function when it gets clicked, so I tried
<a href="./index2.html;javascript:randomFunction();"
but it doesn't work, how can I make an element make switch html page and call a function at once? Thanks!!
Assuming that you want to run the JS on the current page:
The quick and dirty method that you shouldn't use is to add an onclick attribute.
<a href="index2.html" onclick="randomFunction()">
The clean approach is to bind an event handler with JS.
reference_to_anchor.addEventListener('click', randomFunction);
(See the documentation linked above for details, and for work arounds to lack of support in older versions of IE).
(See also Progressive Enhancement and Unobtrusive JavaScript).
This will run the JavaScript on the page on which the link appears before the link is followed.
If, on the other hand, you want the JavaScript to run on the destination page:
You have a rather more complicated problem to deal with.
A new page means a new execution environment. Client side code on one page can't trigger client side code on the next.
I'd approach that problem by having a server side script generate index2 and include <script>randomFunction();</script> if a particular query string was present on the URI.
make it call the desired function via onclick event, at the end of the function do a:
window.location=url;
or if you want to change page and then call a function on the new one use a normal a tag to go to the new page, then:
window.onload=function(){
//do something
}
You can use onclick="javascript:randomFunction()"
For instance:
<a href="./index2.html" onclick="randomFunction()">
use onclick event to call a function
you can use onclick event of javascript.
function randomFunction(){
// do needfull code here.
}
I'm trying to figure this one out but my mind has just gone blank.
I have a button element on my webpage with an id: e.g <button id="someId"></button>
In the document.ready function, I have an on click event which occurs when the user clicks the button. e.g. $('#someId').on('click', function() {
In this event there is an if statement which determines a value. Depending on the value, I want to execute a href in an anchor but I can't figure out the syntax.
The reason I am trying to execute the anchor in the javascript is because i'm passing a variable into the href. It could be true or false.
Here is what I have\what I'm trying to do. Any help would be great.
if (date1 <= date2) {
//I want to execute this anchor
}else{
//otherwise execute this anchor
}
You're looking at the problem wrong. An anchor is a static HTML element that, when clicked, changes the current window location (i.e. URL). The JS equivalent looks something like this: window.location.href = 'http://www.google.com';. So by setting window.location.href you will use JS to navigate to another URL.
The window.location method posted by Eli is the correct way to visit a link using JavaScript. However, if you can use a link instead of a button, you could just set the href of the link in the onclick. Here is a sample jsFiddle that visits a different url based on whether one input is greater than the other:
http://jsfiddle.net/jw3Pd/
So when the user clicks the link, set the href to whatever link you want the user to visit.
$("#someId").on("click", function () {
if (date1 <= date2) {
//I want to execute this anchor
$("#someId").attr("href", "#{Application.openthispage(true)}");
} else{
//otherwise execute this anchor
$("#someId").attr("href", "#{Application.openthispage(false)}");
}
});
Let's say, in website, I want to display the notice message block whenever people click any of the link at my website more than x number of times. Is that possible to count with javascript and display the notice message block ? And can we count the refresh times also ? Or can it be only done with server side language like php ? Please kindly suggest. Thank you.
With Regards,
To do something when any link is clicked is best done with JQuery's live:
Description: Attach a handler to the
event for all elements which match the
current selector, now and in the
future.
$('a').live('click', function() {
// Live handler called.
});
Even if you add more links in run time, this will take care of it.
For counting refreshes I would do it with ajax calls on window.load event, or if you want to use new tech - store it locally with Html5. :-)
You can do that on the client. However, this will be limited to the browser. The simplest will be to store this information in cookies on the client. For instance with jQuery you could simply intercept clicks like that:
$("a").click(function() {
var clickedUrl = $(this).attr('href');
// Here you update the cookie for the count of clicks for that A URL
});
I would either count page refreshes serverside or probably call an ajax function to update the count when the page loads.
If you want to count clicks you may need to bind an event to each link and then for each indivisual button store the number of clicks in global variables...
You could register each click event on the document by using:
$(document).click(function()
{
// Check the number in the cookie and add another
// click to the cookie
});
Then you could use the jQuery cookie plugin to store that value and check it each time there is a click (in the function above).
here's the cookie plugin: https://github.com/carhartl/jquery-cookie
I threw together a quick example. If you're not worried about doing this from page to page then you don't need cookies, just store it in a variable:
http://www.webdesignandseo.net/jquery/clickcount/