2013-10-02 273 views
1

我知道這可能看起來很簡單,當我第一次嘗試時,我自己也這麼認爲,但事實證明我有問題。這是一個UsersScript,我希望它檢查URL是否爲「http://orteil.dashnet.org/cookieclicker」,然後做一件事(但不刷新),但如果URL是「http://orteil.dashnet.org/cookieclicker/beta」做這個其他的事情(也不刷新) 。這裏是我到目前爲止的代碼,我只想在「http://orteil.dashnet.org/cookieclicker」時運行「linkb」,在「http://orteil.dashnet.org/cookieclicker/beta」運行時運行「linkc」。如果URL = this {//做到這一點} else {//做到這一點}

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" 

我曾嘗試:

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') 
    ); 
} 

我都試過,但有警報()的,當你在彈出推確定,它刷新頁面,並再次做了警報。我只是想要它檢查它是什麼URL並做相應的代碼。

如果有人可以想出一些想法,甚至可能是一個解決方案,它將不勝感激。

謝謝,丹尼爾

回答

0

是應該if (window.location == "http://ortei.dashnet.org/cookieclicker/")(雙=),否則你分配URL到window.location的,從而迫使一重裝

+0

哇... brainfart ......這個節目如果你不睡36個小時,你會有多糟糕......感謝兄弟們:) –

+0

@ZacktonJochem,如果這解決了你的問題,你應該[勾選此答案旁邊的複選標記](http:///meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235)。 –

+0

噢,是的,對不起,我再次在37個小時沒有睡覺的時候指責它:/ –