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

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top