I want to calculate time consumed in redirecting from 1 webpage to another webpage.
For Example:
1) I am using Facebook in Google Chrome browser.
I have shared 1 link on my Facebook profile like below:
http://www.webdeveloper.com/
(It's not only Facebook. It can be any domain having link to another domain).
2) When I click on this link from my Facebook profile, then this website will open in new tab.
3) I want to calculate time difference in miliseconds or microseconds between below two events:
First Event: Time of clicking link "http://www.webdeveloper.com/" from my Facebook profile.
Second Event: Time of completely loading webpage of "http://www.webdeveloper.com/".
Thank you in advance.
Unless you load the linked page yourself into a frame or with xmlhhtp request, your facebook page does not have control of another page. In other words, as soon as the user clicks the link you have no control and it runs separately. If you use a frame or load the page ajax style with javascript into an object, it's not going to give you the same kind of timing. So this is basically a pointless excercise as you can't do it. You could potentially setup your own browser with whatever analyzer so it will give you timings but you can't set up any code that would time it for visitors, for the aforementioned reason. If it was possible to do such things then this code could also manipulate the linked to page and take over it. With such lax security you couldn't trust any link you click.
Related
I'm trying to write a script (bookmarklet, really) which has two parts. At the end of the first part, I want to click a button on the page which takes me to a second page. I then want the script to continue running after the second page has loaded. Is this possible?
Every web page load is treated separately by the browser, so there is no way to get a script to continue running where it started off. Here are a few solutions though:
1) Save state to cookies, then read the cookies from the script on the second page to pick up where you left off. For instance, you could save the user's name "John Doe" to a cookie in the first page, then the script in the second page could load the user's name from the cookie. This is probably what you'll want to end up doing.
2) Instead of loading a new page in the browser window, load your new page in an iFrame, and the script running in your outer window won't be interrupted. You can reach inside iFrames with JavaScript as long as they are on the same origin.
You aren't able to load a new page and continue a script from the previous page. But you could ajax load the new page, use the history API to modify the URL, and your script would keep running.
I want to know, if it is possible and how to run javascript code in console in new page, when somebody click on link in my page.
For example. I have got page and there is button. When somebody click on it, it will redirect to another page. In this page It will run my own javascript code, sended from previous page and appeared in console of new page.
This way I want to change for example stylesheet of some selected page and demonstrate the changes.
Thank you.
Shortest answer: No.
Short answer:
This would effectively violate the same-origin policy, and would be a nightmare as far as XSS is concerned.
Imagine for a moment that someone created an 'infected' link, when clicked would infect all the links on the next page in the same manner. Eventually when on the correct page, these infected links would run some code that would snatch up details of what was occurring inside the page (keystrokes, clicks, etc...) and send it back to a server.
All this without ever touching the user's machine.
The closest thing you can do is create a bookmarklet or userscript. It's up to the user to place these things on their computer, and run them on the correct page.
I'm implementing google analytics into a website. This website has a page A that has two links to page B (on same domain).
I want track with GA which navigation the user used to come from page A to page B.
As far as I understand the google's devguides it's only 100% tracked when I use hitCallbacks.
Is this correct or are there better solutions than do a javascript redirect after GA event was successfully sent?
If you have the GA tracking code on both pages, there is no need to use hitCallbacks. Simply add different events to each link from page A to page B.
In my experience, hitCallback is most useful for tracking outbound links - when the user clicks a link that leaves your site. On an outbound link, the new page often loads before your GA script has time to run and register the event. hitCallback solves that problem by ensuring that the event registers before the new page is loaded.
However, when the user is moving from one page to another in your own domain, GA will register the event while the user is on Page B (assuming it didn't get registered on page A). GA puts the event into its queue before moving from A to B. Once B has loaded, the GA script will process the queue and send the event to Google. This is the asynchronous nature of the GA script. GA went asynchronous back in 2009, and this article from that time give a good explanation of how it works.
The reason that this does not work on outbound links is that when the user's browser leaves your domain, the information in his browser's Javascript global variables is lost - that includes the events in the GA queue that have not yet been transmitted to Google. In such situations, hitCallback can be used to force transmission before leaving your domain.
I believe you are looking for something called Link Attribution.
From the page:
You can tag your pages to implement an enhanced link-tracking
functionality that lets you:
See separate information for multiple links on a page that all have the same destination. For example, if there are two links on the
same page that both lead to the Contact Us page, then you see separate
click information for each link.
See when one page element has multiple destinations. For example, a Search button on your page is likely to lead to multiple
destinations.
Track buttons, menus, and actions driven by javascript.
To enable this, you simply need to add one of the following lines depending on which version you are using:
analytics.js/Universal Analytics:
ga('require', 'linkid', 'linkid.js');
ga.js/Legacy:
var pluginUrl =
'//www.google-analytics.com/plugins/ga/inpage_linkid.js';
_gaq.push(['_require', 'inpage_linkid', pluginUrl]);
A website contains a "random" link, which loads a url that returns a 307 redirecting to the url we want. It works fine: click it and you load a random page. The problem is that each time you click it, the browser assumes you're loading the same page: so if you're on the homepage, then you follow the random link 5 times, then you press back, you'll be taken all the way back to the homepage, with no way to find the random pages you were just looking at. I want to modify this behavior so that users can access previous random pages via the back and forward buttons.
I don't own the website, so I can't just change the redirect code.
Here's what I've tried, all of which has failed.
Predicting what would be redirected to. While somewhat possible, there would be no way to avoid failure in up to .1% of clicks, and it would react very poorly to unexpected events, like a page that's published a day late, let alone a sit structure change.
Loading the 307 page via ajax. The request stops at readystate == 2 and I can't access the location header.
Cancel the click event and instead set location.href = random_link.href. This has no effect - the new page still doesn't go into history.
Have the new page call history.pushState. This successfully adds the page to history, but I can't find a way to distinguish between new pages and ones being opened via the back button, so the history quickly becomes very corrupted.
Keeping my own history in localStorage. As above, I can't tell when the back button is being used.
I'm working on a solution that I'm pretty sure will work, involving loading the page in an iframe over the existing page and using a background process and messaging to work around the fact that content injections from chrome extensions can't access window.parent from within iframes. And using the history API to reflect the current iframe's URL in the address bar, and get the back and forwards buttons to apply to the current iframe where appropriate.
While I'm pretty sure the last solution can be made to work, it's a hideously complex and heavyweight approach to what seems like a simple problem. So I thought I'd ask you guys before I continue: any other ideas?
Have you tried storing the locations in localStorage, then hi-jacking the back button ?
I am sure you know how localStorage works, for hi-jacking the back button you can refer to this : Is there a way to catch the back button event in javascript?
T.
Here's what I need to do. I'm using Google Chrome. I have page that auto-reloads every 5 seconds using a script:
javascript: timeout=prompt("Set timeout [s]");
current=location.href;
if(timeout>0)
setTimeout('reload()',1000*timeout);
else
location.replace(current);
function reload()
{
setTimeout('reload()',1000*timeout);
fr4me='<frameset cols=\'*\'>\n<frame src=\''+current+'\'/>'; fr4me+='</frameset>';
with(document){write(fr4me);void(close())};
}
I found that script by Googling.
The reason why the page auto-reloads every 5 seconds is I'm waiting for a specific link or url to appear in the page. It appears at random times. Once I see the link I'm waiting for, I immediately click the link. That's fine.
But I want more. What I want is the page will auto-reload and I want it to auto-detect the the link I'm waiting for. Once the script finds the link I'm waiting for, it automatically loads that link on a new tab or page.
For example, I'm auto-reloading www.example.com. I'm waiting for a specific url "BUY NOW". When the page auto-reloads, it checks if there's a url "BUY NOW". If it sees one, it should automatically open that link.
Thanks.
For inspiration, check out what I have done it in my Chrome Extension "Auto-Reload". The code is here.
You should create yourself an extension (from what I understand of your question, that's what you are implying). Once the page reloads, you can use jQuery (for example) to scrap the page for your link.