I have a website with embedded videos. Each iframe comes from different website.
The problem is that some of them, are redirecting my visitors to their assigned destination without actions (click, hover, etc). And now Google is listing my website as suspicious.
Is there any php script to track every single exit link from my pages to another website?
Let's say i'm using Wordpress and have already created $table_name tracking 1-timestamp, 2-my page that was redirected, and 3-the destination of the external website/domain.
function the_function() {
// if there is any function
}
if ( the_function() == true // or false )
{
$wpdb->insert($table_name, array(
'timestamp' => $timestamp,
'page' => $my_page_redirected_to_a_different_domain,
'destination'=> $destination,
));
}
Or if there is any js script, no problem
There is no way to track down redirects in a general way - you have to trigger an event right before the redirect is processed, so you have to know how this happen.
Why ? Because a JS redirection is processed by the browser and not on the server side - which means the only way to trigger an SQL insert is to know how the redirection is triggered. This is basically what you want to achieve:
What you can do to locate the redirection
Search for every occurences of window.location in your JS files - you could search for the URL that you've been redirected too (all your local js files and the external ones you include)
Disable (with comments) the external <iframe> you're using. An iframe can do a window.top.location.href to redirect the main page.
An other way to redirect the page would be to trigger a click event on a link through JS. I guess by searching for the redirect URL in your js file you could locate that too.
Last, if the redirection happens before the page is load, it could a PHP redirection - looking for header calls that redirect to the URL in your php files would catch that.
What can be done when the redirect is found
It's a JS redirection (window.location)
If it's inside a local file, you can just comment it if you want to disable it, if you want to keep it but track it, add this code right before the window.location call (it's jQuery, it will make an AJAX request to /log-redirects.php) :
$.post( "/log-redirects.php", function( data ) {
// ADD the window.location line here
});
It's really important that you move the window.location call inside the AJAX callback, so the redirection will be made only when the ajax call is finished.
If the redirection happens in an external js, you could not probably do anything besides removing the whole JS file - maybe get the JS file content, remove the redirection and include it locally.
One thing you could try is to add a listener on the beforeunload event - though you'll have to do the correct check on the event object to see if it's the redirection you're looking for, as it'll catch any unload events.
window.addEventListener("beforeunload", function (e) {
// do something
return false; // will result to a prompt to the user asking him if he really want to redirect
// return this only for the redirect you're looking for
});
Please note that this method doesn't seems to be completely reliable.
It's from an <iframe>
You can add the sandbox attribute to the iframe to prevent it to do any redirect. Please note that this attribute is not supported in IE9 and lower.
<iframe src="http://youriframeurl.com/" sandbox=""></iframe>
It's a click event triggered through JS
You can do the same thing as for the first point. You just want to add a listener to the link (using on if it's generated after DOM is load) :
$('a#mylink').on('click', function(e) {
// do something
e.preventDefault(); // add this if you want to cancel the redirection
});
It's a header redirection (PHP)
That's an easy case - just add the code you want before the header call.
Conclusion
I suggest to work first with the beforeunload event to track the redirection - doing a console.log(event); could give you some insights on the cause of the redirection.
Good luck!
Related
I'm fairly new to javaScript & would like to know if is possible to use window.location.reload() on a page to reload all contents on a page but one? I've researched google but to no avail.
To answer your question, no it is not possible to reload the page partially with window.reload()
What you could do though is fetched the HTML page and use it to replace partial content on your page. It would be better if your backend could serve already the parts of your page you need individually, so you only have to update the content of the page instead of parsing some HTML first, but it can work both ways.
For example from one sample project I made. This will fetch periodically some HTML from the backend and update the page partially.
You can see the full thing here https://repl.it/#bluebrown/SSR-Dataframes#templates/dashboard.html
const fetcher = Object.entries(frames).map(([id, {refresh_rate}]) => {
function refresh() {
fetch(`/df/${id}`)
.then((bytes) => bytes.text())
.then((html) => document.getElementById(id).innerHTML = html)
.catch(console.warn)
return setTimeout(refresh, refresh_rate*1000)
}
return refresh();
})
This works so well because the backend and the frontend know each other well.
In other cases you may want or need to fetch a full page and parse it with something like jsdom. Then you can grab from that DOM what you need and update the right element on your page.
The location.reload method reloads the current document, just like the user pressed F5 or the browser reload button.
The default setting reloads the page from browser cache, if it is available. You can force the method to bypass local cache and retrieve the document from the network by passing true to the method.
location.reload(); //refreshes from cache //or location.reload(true); //to force a network request
You can not do that, location.reload will reload the current document. Only way I can think, and this may not work with your page design, you can put the different contents in separate iframes, and reload only those iframes that needs reload/refresh.
i make a single page application. I had read the following article create-crawlable-pushstate.
I run just into problems using hashbang. This seems for me like a solution. But im not quite sure if i userstand what is going on there. Here is an example from the article :
// We're using jQuery functions to make our lives loads easier
$('nav a').click(function(e) {
url = $(this).attr("href");
//This function would get content from the server and insert it into the id="content" element
$.getJSON("content.php", {contentid : url},function (data) {
$("#content").html(data);
});
//This is where we update the address bar with the 'url' parameter
window.history.pushState('object', 'New Title', url);
//This stops the browser from actually following the link
e.preventDefault();
}
Thats great but how will google know that the content is available. The getJson function is asynchronous, so the state will be pushed before the content is loaded. My thought was that i call pushstate after content is loaded to show that the link is ready.
In my scenario a user clicks on a href.
Router catch hash change and call a function. (I can overwrite the click event to pushstate after content is generated.
Content will be loaded and generated. (Time pass)
Google doesn't care about the JavaScript. All it sees is the URLs.
Your server still needs to generate the appropriate page for each given URL.
With JS:
Client (browser, googlebot, whatever) asks for http://example.com/whatever/whatever/whatever
Server responds with the page for /whatever/whatever/whatever
Client clicks on link with:
href="/something/something/something
Ajax</li>
<li>preventDefault`
Client loads content with Ajax
Client changes URL to /something/something/something without reloading the whole page
Without JS:
Client (browser, googlebot, whatever) asks for http://example.com/whatever/whatever/whatever
Server responds with the page for /whatever/whatever/whatever
Client clicks on link with:
href="/something/something/something
Ajax</li>
<li>preventDefault`
Client goes to /something/something/something (since there is no JS to trigger the preventDefault)
Then later, someone goes directly to /something/something/something and the same applies. The server delivers the content for /something/something/something directly. Then JS does Ajax stuff when the click a link (if JS is available).
Maybe that's not a good question but I'm wondering if it would be somehow easily and quickly (with just 1 function perhaps?) possible to change ALL links on website from refreshing webpage to loading the URL that user clicked via AJAX?
My website has standard links everywhere but if you want to have mobile application for iPhone out of it you need to use AJAX everywhere (and perhaps HTML5 History API) because any link will open Safari browser.
Do you think there's any way to quickly convert every single link to delete current source code and load brand-new page without refreshing browser window? Or does it require manual coding and separate functions for every single set of links?
Example:
jQuery(document).on('a', 'click', function(){
// STEP1: AJAX call that will make PHP download page from link
// STEP2: Delete current source code and load new one (in this case including deletion of this function itself)
});
Do you think it would be possible or are there any pitfalls here?
I've done something similar to this. It's possible, but you may have to change a couple of things on server side.
In my case, i had some links with href = #, some of them had click triggers, so i wanted to keep them out.
In this function i check every link without href = #, and if they don't have a click event, i bind ajax.
bindAjaxLinks: function() {
$('a[href!="#"]').each(function() {
var eventObj = $(this).data('events');
if (!eventObj || !eventObj.click) {
// not a javascript link, do your thing
var link = $(this).attr('href');
if (link) {
$(this).click(function(event) {
event.preventDefault();
// do your ajax calling here.
});
}
}
});
Some considerations: In my case, i added an extra parameter, something like isAjax to every request i made with this function, and in backend i sent only a part of whole page(without head and some other markup). And later i replaced what is visible to user with this new html.
This caused a problem: if server responded with a redirect, i got the whole page, which i didn't want, so i made sure this isAjax parameter is kept after redirects, and that solved this problem.
In Chrome, I'm looking to detect in a page URL is going to be on example.com's domain, and if it is, before loading, append foo=bar as a parameter and load that instead.
I've found that I can access when the Omnibar has been submitted here, but it seems like it'd load the original URL anyways, and while that's alright it's twice the bandwidth I feel is necessary. It's not a problem when it's only one page, but it's a change that needs to happen on every page in an site, so double the bandwidth definitely becomes an issue.
Currently, it works to detect if the URL is going to be example.com and is submitted, then call window.stop() and then set location.href to example.com/&?foo=bar but that doesn't seem ideal.
In short, the user goes to http://www.example.com and then the script changes it to http://www.example.com/&?foo=bar before loading the original link.
Take a look at the chrome.webRequest API, in particular the following method:
onBeforeRequest (optionally synchronous)
Fires when a request is about to occur. This event is sent before any TCP connection is made and can be used to cancel or redirect requests.
You can use
window.location.search = "foo=bar"
may be this helps.
I've got a client who is setting up a system where a certain segment of visitors to a page "A" will receive a Javascript redirect to another page "B". (I know, not ideal but not my idea...)
They're using a Javascript tag-based web analytics solution (Omniture SiteCatalyst) which is deployed on both pages.
My question is, for visitors who we redirect, can we ensure that the redirect will fire and that they'll be "off" of page "A" before the page "A" web analytics code fires and triggers a page view?
Is there something that needs to be done programatically, and is there a more or less "foolproof" way to make sure that page "A"'s analytics code won't fire, or will any solution have some leakage depending on variations in browsers and net / client PC speed and so forth?
One way you can be certain is to return the JavaScript code that does the redirection, and nothing else. Is there any reason to load the contents of the page when the user will just be redirected, anyway?
In other words, in the server-side language of your choice:
if in experiment B:
emit javascript redirection template
else:
emit template for page A
Where, the JavaScript redirection template is nothing more than:
<script>window.location.href="path/of/page/B";</script>
You'd have to structure the code to look something like this (pseudocode):
if( on_page_a ){
if( user_requires_redirect ){
redirect( PAGE_B );
} else {
fire_analytics( PAGE_A );
}
} else {
fire_analytics( PAGE_A );
}
Hope that makes sense