2013-01-25 146 views
0

我一直在嘗試開發Safari Firefox和Chrome的一個簡單擴展,其中一種形式在彈出窗口中呈現給用戶,並且在填寫之後被本地評估以及對用戶在該評估上定義。 原始形式,可以發現:https://dl.dropbox.com/u/86545493/Waze/Calculadora.htmljavascript格式評估的格式消息

我也已經能夠創建工作的Safari擴展,可以在這裏下載:https://dl.dropbox.com/u/86545493/Waze/Extensions/WRTC.safariextz 而是試圖創建Chrome擴展時,我的問題starte。

我按照教程創建了第一個擴展,並能夠創建一個按鈕並將該窗體作爲彈出窗口加載。但是,安全限制不允許我的表單運行。從我通過閱讀開發者指南得知的內容,我需要的是將JavaScript分離到它自己的文件中,並從彈出文件中調用它。然而,這些文件的語言對我來說太過於技術(醫學生),我一直無法理解格式化應該使用的消息的正確方法。這裏是我的代碼的當前狀態:

popup.html

<head> 
<script type="text/javascript" src="popup.js"></script> 
</head> 
<body> 
<h1>Calculadora de Tipo de Camino</h1> 
<form id="Calle" target="_self"> 
    Cruceros <select id="cruceros"> 
    <option value=1>Glorietas</option> 
    <option value=1>Rotondas</option> 
    <option value=1>Sem&aacuteforos</option> 
    <option value=0 selected>Sin Control</option> 
    <option value=2>Sin Cruceros</option> 
    </select></br> 
    Carriles <select id="carriles"> 
     <option value=1 selected>1</option> 
     <option value=2>2</option> 
     <option value=3>3 o m&aacutes</option> 
    </select></br> 
    Sentidos Separados f&iacutesicamente o parte de par vial <input type="checkbox" id="sentidos"></br> 
    Con Laterales o Acotamiento <input type="checkbox" id="laterales"></br> 
    Entradas y Salidas delimitadas <input type="checkbox" id="ramps"></br> 
    Avenida, Boulevard, o Circuito <input type="checkbox" id="name"></br> 
    Es Carretera 
    <select id="carretera"> 
     <option value=0 selected>No</option> 
     <option value=1>Municipal</option> 
     <option value=2>Estatal</option> 
     <option value=3>Federal</option> 
    </select> Revisar en: <a href="http://petra.sdg.mx/sigcsi/" target="_blank">SCT</a></br> 
    Tiene topes <input type="checkbox" id="topes"></br> 
    </br> 
</form> 
<button type="button" onclick="textR()">Calcular Puntos</button> 
<hr><p id="resultn"></p> 
<p id="resultt"></p> 
</body> 

popup.js

function textR() 
{var cru=document.getElementById("cruceros").value; 
if (cru==0){crun=0} 
else if (cru==1){crun=1} 
else{crun=2} 
var carr=document.getElementById("carriles").value; 
if (carr==1){carrn=0} 
else if (carr==2){carrn=1} 
else{carrn=2} 
sen=(document.getElementById("sentidos").checked==true)?1:0; 
lat=(document.getElementById("laterales").checked==true)?1:0; 
ram=(document.getElementById("ramps").checked==true)?1:0; 
av=(document.getElementById("name").checked==true)?1:0; 
var hw=document.getElementById("carretera").value; 
if (hw==1){hwn=1} 
else if (hw==2){hwn=2} 
else if (hw==3){hwn=3} 
else{hwn=0} 
saluden=(document.getElementById("topes").checked==true)?1:0; 
var r=crun+carrn+sen+lat+ram+av+hwn+saluden; 
document.getElementById("resultn").innerHTML=(r+" puntos"); 
if (r<2){rt='Street'} 
else if (r<4){rt='<span class="pstreet">Primary Street'} 
else if (r<7){rt='<span class="mihw"> Minor Highway '} 
else if (r<9){rt='<span class="mahw"> Major Highway '} 
else {rt='<span class="fw"> Freeway '} 
document.getElementById("resultt").innerHTML=(rt); 
} 

你能告訴我正確的語法使用?

回答

0

消除內嵌代碼onclick="textR()"和這裏

document.getElementsByTagName("button")[0].addEventListener("click",textR); 
+0

其實我不得不使用的getElementById而不是因爲「ByTagName」產生了一些其他錯誤,我也不得不對代碼做更多的變化。 – Edsonytic