-1

I am currently using div id for users to go to my site and jump to div (ie sitename.com/sitepage/#div_1). My issue is that if these urls are used adsense recognises this as autoscrolling and therefore displays the ad in a double-click format (ie user has to click twice to go to advertiser site as opposed to normal click once).

I do not want to use scrollto script. However, I have found popstate is close to what I want but because the desired effect isn't the same across all browsers I want to use something simple like:

setTimeout(function() {
  window.location.href = "#div1";
}, 1000);

The above will complete step 1 of my desired end result. However, I have multiple urls eg

sitename.com/sitepage/#1
sitename.com/sitepage/#2
sitename.com/sitepage/#3

Is there a way to recognise that if url contains # than insert "div" to url and respective number so it ties in with above code. My hope is something like below:

setTimeout(function() {
    if(window.location.href.indexOf("#") > -1) {
      window.location.href = "#divn";
    }, 1000);

But I don't know how to say add "div" after "#"

Manny
  • 59
  • 8
  • That is unclear... Can you reword your question as to split the two issues appart? There is a scroll issue using the URL hash... And a click/double-click isue on something. How are those two issue linked? – Louys Patrice Bessette Jan 22 '18 at 01:34
  • The actual issue is adsense is not loading properly as a single click frame when I use #url (it loads as a frame where user has to double-click to go to advertiser site). I have tried various options but none of them work.Is there a solution to this where adsense script doesn't recognise the "jump" on page when using # – Manny Jan 22 '18 at 17:27
  • If the hash (# sign in the URL) is the Adsense `iframe`... You could try to force `focus` on it, on load. *Maybe* that would save the first user click... I don't know, but try `$(document).ready(function(){ $(location.hash).focus(); });` – Louys Patrice Bessette Jan 22 '18 at 18:05
  • No doesn't work unfortunately. Focus() would cause it to jump straight away and this is the same as using sitename.com/#sitepage. I've edited my question as I'm getting near my goal – Manny Jan 22 '18 at 18:41
  • Using the hash WILL jump straith there on load... Isn't it what you want? – Louys Patrice Bessette Jan 22 '18 at 18:43
  • Thanks for the reply Louys. Have changed my initial question. That is what I want but adsense doesn't like that so I'm trying to create a delay so adsense loads first and then jump occurs. I don't want to use scrollto at all. – Manny Jan 22 '18 at 19:01
  • mmm... Try `if(window.location.hash != null) {` instead of `if(window.location.href.indexOf("#") > -1) {` --- Then, do not reload the page again! `window.location.href = "#divn";` is no good. Try the focus : `$("#divn").focus();` – Louys Patrice Bessette Jan 22 '18 at 19:05

1 Answers1

1

EDIT

Another idea that should work this time!

The issue is about to prevent the hash action to scroll...
What if the id in the hash does not exist ?
The scroll would not occur, right?

Try something like domain/path/file#target99 assuming the id #target99 does NOT exist on the page... But id #target does.

The 99 part can be anything... It's as you wish. That is the part you will REMOVE!

if(window.location.hash != null) {
  var hash = window.location.hash.replace("99","");  // Get the hash value WITHOUT the 99

  if(hash != "#"){  // in case of only "self"
    setTimeout(function() {
      $(hash).focus();
    }, 1000);
  }
}
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
  • Thanks but it comes with a wierd parameter at the end of the url **/#[object HTMLDivElement]** – Manny Jan 22 '18 at 19:15
  • if I remove 'null' than it just loads as normal ie jumps straight to div – Manny Jan 22 '18 at 19:25
  • Thanks again. It doesn't work. I'm attempting to splice and insert div after # if you can point me to a working code that'd be much appreciated currently I'm struggling to string it together. ie [link]https://stackoverflow.com/questions/8737615/append-a-param-onto-the-current-url – Manny Jan 22 '18 at 21:26
  • I don't get what you're actually trying... I assumed the hash was already in the URL. So `location.hash` should give it. Try `console.log(location.hash);` The `#` is there. The thing is about to prevent the "normal" hash action to scroll to the div. That's how I understand your issue. -- But *maybe* it's not possible. – Louys Patrice Bessette Jan 22 '18 at 21:37
  • 1
    Thanks for the support. The idea works appreciate the help :) – Manny Jan 25 '18 at 16:04