1

I know this may seem simple, I thought so myself when I first tried it, but it turns out i'm having issues with it. This is a Userscript and I am wanting it to check if the URL is "http://orteil.dashnet.org/cookieclicker", then do one thing (but don't refresh) but if the URL is "http://orteil.dashnet.org/cookieclicker/beta" the do this other thing (also don't refresh). Here is the code that I have so far, I am just wanting to get "linkb" to run when "http://orteil.dashnet.org/cookieclicker" and "linkc" to run when "http://orteil.dashnet.org/cookieclicker/beta".

var link = document.createElement('a');
link.setAttribute('href', 'http://orteil.dashnet.org/experiments/cookie/');
link.target = 'blank';
link.appendChild(
   document.createTextNode('Cookie Clicker Classic')
);
var add = document.getElementsByTagName('div')[1];
add.insertBefore(document.createTextNode('| '), add.lastChild);
add.insertBefore(link, add.lastChild); // all the code so far will load on both pages

var linkb = document.createElement('a');
linkb.setAttribute('href', 'beta');
linkb.target = 'blank';
linkb.appendChild(
   document.createTextNode('Try the beta!') //this block will load if the URL is "http://orteil.dashnet.org/cookieclicker"

var linkc = document.createElement('a');
linkc.setAttribute('href', '../');
linkc.target = 'blank';
linkc.appendChild(
   document.createTextNode('Live version') // and this will load if the URL is "http://orteil.dashnet.org/cookieclicker/beta"

I have tried:

if (window.location = "http://ortei.dashnet.org/cookieclicker/") {
   var linkb = document.createElement('a');
   linkb.setAttribute('href', 'beta');
   linkb.target = 'blank';
   linkb.appendChild(
      document.createTextNode('Try the beta!')
   );
}
else {
   var linkc = document.createElement('a');
   linkc.setAttribute('href', '../');
   linkc.target = 'blank';
   linkc.appendChild(
      document.createTextNode('Live version')
   );
}

I have tried this but with alert()'s and when you push ok on the popup, it refreshes the page and does the alert again. I just want it to check what URL it is and do the corresponding code.

If anyone could come up with some ideas, or even possibly a solution, it would be much appreciated.

Thanks, Daniel

1 Answers1

0

Is should be if (window.location == "http://ortei.dashnet.org/cookieclicker/") (double =), otherwise you're assigning the URL to the window.location, thus forcing a reload

Thor Jacobsen
  • 8,621
  • 2
  • 27
  • 26
  • Wow... brainfart... this shows how bad you can get if you don't sleep for 36 hours... thanks dude :) – Zackton Jochem Oct 02 '13 at 07:01
  • @ZacktonJochem, If this solved the problem for you, you should [tick the check-mark next to this answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235). – Brock Adams Oct 02 '13 at 07:16
  • Oh yes, sorry about that, again i'm blaming it on the 37 hours without sleep :/ – Zackton Jochem Oct 02 '13 at 07:24