Results 1 to 10 of 10

Thread: add variable to url after ad blocking detected

  1. #1
    Join Date
    May 2017
    Location
    Dallas
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default add variable to url after ad blocking detected

    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.

    PHP Code:
    <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> 

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,407
    Thanks
    77
    Thanked 3,432 Times in 3,393 Posts
    Blog Entries
    12

    Default

    Try:

    Code:
    <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>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    May 2017
    Location
    Dallas
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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?

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

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,407
    Thanks
    77
    Thanked 3,432 Times in 3,393 Posts
    Blog Entries
    12

    Default

    As for this bit of code:

    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?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    May 2017
    Location
    Dallas
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,407
    Thanks
    77
    Thanked 3,432 Times in 3,393 Posts
    Blog Entries
    12

    Default

    So it's working the way you want it to now?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    May 2017
    Location
    Dallas
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  8. #8
    Join Date
    May 2017
    Location
    Dallas
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

    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.

  9. #9
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    440
    Thanks
    0
    Thanked 55 Times in 51 Posts

    Default

    Quote Originally Posted by Simplyput View Post
    .
    .
    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.

  10. #10
    Join Date
    May 2017
    Location
    Dallas
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by styxlawyer View Post
    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.

Similar Threads

  1. Rainbow Links being detected as malware/spyware
    By fadedangel416 in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 08-14-2009, 08:22 PM
  2. Replies: 1
    Last Post: 03-05-2008, 10:44 PM
  3. Display Specific Text Based on Time Detected?
    By wpthebest in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 03-13-2007, 10:29 PM
  4. Using Image If NO FLASH detected...
    By tomyknoker in forum JavaScript
    Replies: 3
    Last Post: 05-23-2006, 06:35 AM
  5. Wrong Country Detected
    By stephen_wq in forum The lounge
    Replies: 3
    Last Post: 06-05-2005, 07:20 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •