2016-06-22 33 views
0

我一直試圖通過多種查看方式使我的代碼功能很好,例如Web ArchivePHProxy。下面是代碼:將我的代碼加載到Web上存檔和JavaScript無法正常工作

<select id="mySelect" OnChange="window.location.href=this.value"> 
    <option>Tagged Stuff and Navigation</option> 
</select> 

<script> 
    if (window.location.href.substring(0, 9) === "http://th") { 
     var home_var = "/"; 
     var bio_var = "/tagged/bio"; 
     var ask_var = "/ask"; 
    } else if (window.location.href.substring(0, 9) === "https://p") { 
     var home_var = "https://px.multiscreensite.com/index.php?url=blog.tumblr.com"; 
     var bio_var = "https://px.multiscreensite.com/index.php?url=blog.tumblr.com/tagged/bio"; 
     var ask_var = "https://px.multiscreensite.com/index.php?url=blog.tumblr.com/ask"; 
    } else if (window.location.href.substring(0, 9) === "https://w") { 
     var home_var = "blog.tumblr.com"; 
     var bio_var = "blog.tumblr.com/tagged/bio"; 
     var ask_var = "blog.tumblr.com/ask"; 
    } 
    var select = document.getElementById("mySelect"); 
    var option = document.createElement("option"); 
    var option1 = document.createElement("option"); 
    var option2 = document.createElement("option"); 

    option.text = "Home Page"; 
    option.value = home_var; 
    select.add(option); 

    option1.text = "Autobiographys"; 
    option1.value = bio_var; 
    select.add(option1); 

    option2.text = "Ask Me Stuff"; 
    option2.value = ask_var; 
    select.add(option2); 
</script> 

我的具體問題是通過Web歸檔打開我的頁面時,變量從來沒有得到正確定義,因此所有的值的問世爲「不確定」。這是Web檔案的問題,還是無論如何,我可以解決這個問題?

回答

1

window.location.href.substring(0,9)不包含匹配的字符串。 該條件未被修改,並且默認值也沒有定義,因爲這個原因該值出現爲未定義。

檢查與

console.log(window.location.href.substring(0, 9)); 

這是我已經試過

<select id="mySelect" OnChange="window.location.href=this.value"> 
    <option>Tagged Stuff and Navigation</option> 
</select> 

<script> 
    var home_var = ""; 
    var bio_var = ""; 
    var ask_var = ""; 
    // Check your variable 
    console.log(window.location.href.substring(0, 9)); 
    if (window.location.href.substring(0, 9) === "http://th") { 
     home_var = "/"; 
     bio_var = "/tagged/bio"; 
     ask_var = "/ask"; 
    } else if (window.location.href.substring(0, 9) === "https://p") { 
     home_var = "https://px.multiscreensite.com/index.php?url=blog.tumblr.com"; 
     bio_var = "https://px.multiscreensite.com/index.php?url=blog.tumblr.com/tagged/bio"; 
     ask_var = "https://px.multiscreensite.com/index.php?url=blog.tumblr.com/ask"; 
    } else if (window.location.href.substring(0, 9) === "https://w") { 
     home_var = "blog.tumblr.com"; 
     bio_var = "blog.tumblr.com/tagged/bio"; 
     ask_var = "blog.tumblr.com/ask"; 
    } else { 
     //Set Default Values 
     home_var = "default home var"; 
     bio_var = "default home var"; 
     ask_var = "default home var"; 
    } 
    var select = document.getElementById("mySelect"); 
    var option = document.createElement("option"); 
    var option1 = document.createElement("option"); 
    var option2 = document.createElement("option"); 

    option.text = "Home Page"; 
    option.value = home_var; 
    select.add(option); 

    option1.text = "Autobiographys"; 
    option1.value = bio_var; 
    select.add(option1); 

    option2.text = "Ask Me Stuff"; 
    option2.value = ask_var; 
    select.add(option2); 
</script> 
+0

謝謝!我會嘗試。 –