PDA

View Full Version : add variable to url after ad blocking detected



Simplyput
05-01-2017, 02:37 AM
Hello all I am in need of help. Javascript is not my strong suit so I am asking my first of many questions on this board, so please be gentle with me:)
Okay here is the problem. After a lot of goggling and piecing code together I have the following below that somewhat works. What it does is detect when an ad blocker is active and then refreshes the page and adds a variable to the end of the active page url after detection. However, the issue I am running into is that is will not stop refreshing the page and adding the variable to the end of the url. I need it to refresh once and add the variable to the end of the url. Any help you can give will be greatly appreciated.



<script type="text/javascript">
window.onload = function() {
setTimeout(function() {
var url = window.location.href;
if (typeof(window.google_jobrunner) === "undefined") {
window.location.href = window.location.href + "&fkdk"
} else {
console.log("no ad blocking found.");
}
}, 10000);
};
</script>

jscheuer1
05-01-2017, 01:26 PM
Try:


<script type="text/javascript">
window.onload = function() {
var loc = window.location.href;
if(loc.indexOf('&fkdk') > -1){return;}
setTimeout(function() {
if (typeof(window.google_jobrunner) === "undefined") {
window.location.href = loc + "&fkdk"
} else {
console.log("no ad blocking found.");
}
}, 10000);
};
</script>

Simplyput
05-02-2017, 06:20 PM
Thank You jscheuer1

This worked great now I am faced with a new issue so I need it tweeked. When it refreshes it ads the code to the end Yeah. However, when I go to another page the end variable is gone, which is correct behavior, and the code never ads the variable back when adblock is on. How do I get it to refresh once on every new page request. The trigger is the blocked ads. Maybe my approach is off?

Quick question. What does this line do here?



if(loc.indexOf('&fkdk') > -1){return;}

jscheuer1
05-03-2017, 05:12 AM
As for this bit of code:


if(loc.indexOf('&fkdk') > -1){return;}

At that point in the code, the variable loc is a string which has been set to the window.location.href (the current address in the address bar). The indexOf() function is a native javascript string function which will return the zero based index position of one string within another or -1 if the substring is not found. So we are just looking for '&fkdk' in the address. If it's already there (if it's index is greater than -1), we return. Using return in that way within a function exits the current function without doing anything further. Hope that explains that line well enough for you. If not, ask another question or questions about it.

As for your other issue, can you be clearer? Do you want it to refresh once on every page load even if adblock is off? Something else?

Simplyput
05-03-2017, 01:58 PM
Thank you for explaining that the more I learn the better I will get at Javascript.
Now for the code, what I am trying to do is have the code check if adblock is active. If its active add the variable to the end of the url. If a person surfs to another page on my site it should do the same check on the new page and append the variable if needed. In the event adblock is turned off then it does nothing. I thought for this to happen the page had to be refreshed for javascript to kick in and add the variable. Thus the refresh once was added to the code.

jscheuer1
05-03-2017, 04:53 PM
So it's working the way you want it to now?

Simplyput
05-03-2017, 06:45 PM
Yes the code is working on the first initial page that I surf to on my site but when I go to another page it's not working.

Simplyput
05-03-2017, 08:10 PM
FontSize

Okay I figured it out. My one line of code was not sufficient enough to trigger the adblock on every page, so I had to go with an adblocking detecting library to trigger the code. below is the working code.



function adblockDetected(){
var loc = window.location.href;
if(loc.indexOf('&fkdk') > -1){return;}
window.location.href = loc + "&fkdk"

}


Thank you for all your help.

styxlawyer
05-04-2017, 01:47 PM
.
.
If a person surfs to another page on my site it should do the same check on the new page and append the variable if needed.
.
.

JavaScript code is embedded in the page and not stored in the browser. So, the same code needs to be added to every new page on your site.

Simplyput
05-04-2017, 04:42 PM
JavaScript code is embedded in the page and not stored in the browser. So, the same code needs to be added to every new page on your site.

You are right it did need to be added to every page and the code resided in a side box that was on every page but still no dice. The issue was how I was detecting the adblock. The added library took care of that and it's now working like a champ.