2015-02-10 48 views
1

首先,我想承认我对javascript没有任何好处。事实上,这让我很沮丧没有尽头,似乎我不能换我围​​绕它的头,仍然给它一个shot.So我有这样的“JS”文件:为什么我不能在javascript中获得元素的值

var url, tab,myNewTab; 
function init(){ 
    chrome.tabs.query({currentWindow: true, active: true},function(tabs){ 
     url = tabs[0].url; 
     tab = tabs[0] 
     tabId = tabs[0].id; 
     processTab(); 
    }); 
} 

function processTab(){ 
    chrome.tabs.update(tabId, {selected: true}); 
    if (url.substring(0,5) === "http:") 
    { 
     console.log(window.location); 
     url = insert(url , 4 , "s"); 
    } 
    myNewTab = window.open('https://alexanderproxy33.appspot.com','_newtab'); 

    myNewTab.onload = cont() ; 
} 
function insert(str, index, value) { 
    return str.substr(0, index) + value + str.substr(index); 
}; 
function cont() 
{ 
    console.log(myNewTab.document.getElementById("input").value) 
} 
init(); 

有很多事情,本来是可以做得好得多我猜想,但我的问题是这样的:console.log行给了我一个错误原因null.I做了cont()函数,所以它会加载它的元素,所以我可以得到它们(正如许多人明显正确),但仍然没有。接下来的部分是我本以为这触发此上述网站的HTML的一部分:

<body> 
    <div class="box"> 
     <h6>Basic Proxy</h1> 
      <center> 
       <form action="" method="get" accept-charset="utf-8" target="_blank"> 
        <input name="url" type="text" class="txt" id="input" placeholder="type url here.." onfocus="this.value='';" /> 
        <input type="submit" class="btn" value="Go" /> 
       </form> 
      </center> 
      <p class="footer">Instructions: Just type the URL of any web page in the input box above (
       <em>e.g. google.com</em>) and hit Enter.</p> 
      <p class="footer"></p> 
    </div> 
</body> 

在此先感谢

+0

你在做一个Chrome扩展吗? – user1778606 2015-02-10 07:09:47

回答

2

myNewTab.onload = cont() ;你正在执行cont,然后设定它的返回值是不正确的(即undefined)为myNewTab.onload。因此,该行应该是myNewTab.onload = cont;

+1

对不起,延迟应对。谢谢,清除了一切,并另外教会了我一些新的东西。今天离开了。再一次,谢谢 – stonedevil 2015-02-12 20:41:17

相关问题